gpt4 book ai didi

iphone - UILongPressGestureRecognizer - 只识别最后一次按下

转载 作者:行者123 更新时间:2023-11-29 11:05:10 26 4
gpt4 key购买 nike

当用户将手指按在屏幕上时,我想执行一个连续的 Action 。这目前工作正常。但是当用户将第二根手指按在屏幕上时,我希望 UILongPressGestureRecognizer 取消/结束第一次按下并开始服从新的按下。执行此操作的最佳方法是什么?我需要有两个 UILongPressGestureRecognizers 并且当一个触发时将另一个设置为

enabled=NO;enabled=YES;

或者有更简洁的方法吗?

目前,仅使用一个 UILongPressGestureRecognizer,当第二根手指按在屏幕上时,它就好像它甚至不知道它在那里一样。

最佳答案

UILongPressGestureRecognizer 只知道最少的触摸次数 - 默认情况下,一个手指 - 稍后放下另一个手指不会有太大影响。在需要单次触摸的真实手机上尝试此操作会看到初始触摸导致识别器过渡到状态 1。将第一根手指侧向移动或触摸第二根手指会导致过渡到状态 2。抬起第一根手指触地导致转换到状态 3 并结束手势。

我将第二个 UILongPressGestureRecognizer 添加到与上面第一个 View 相同的 View 中,但为这个 View 提供了最低双点触控要求。像这样:

    UILongPressGestureRecognizer *lpgr1 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgr1Method:)];
UILongPressGestureRecognizer *lpgr2 = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpgr2Method:)];
lpgr2.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:lpgr1];
[self.view addGestureRecognizer:lpgr2];
// ...
- (void)lpgr1Method:(UIGestureRecognizer *)gestureRecognizer {
UIGestureRecognizerState state = gestureRecognizer.state;
self.stateLabel.text = [NSString stringWithFormat:@"%d", (int)state];
}
- (void)lpgr2Method:(UIGestureRecognizer *)gestureRecognizer {
UIGestureRecognizerState state = gestureRecognizer.state;
self.stateLabel2.text = [NSString stringWithFormat:@"%d", (int)state];
}

如果我先用一根手指触摸并按住它,lpgr1 会进入状态 1。如果我随后用第二根手指触摸并按住它,lpgr1 会进入状态 1状态 2。lpgr2 根本不触发。

如果我同时用两根手指向下触摸并按住它,lpgr2 会触发,而 lpgr1 永远不会触发。

所以看起来向整个 View 添加两个识别器只是令人困惑,并且不会达到您想要的结果,无论您如何编程。因此,我怀疑正确的方法是编写您自己的 UIGestureRecognizer 子类。

编辑:我也试过这个:

lpgr2.delegate = self;
lpgr1.delegate = self;
// ...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return true;
}

这确实允许两个识别器同时触发,并识别出第二根手指已经触碰。然而,lpgr1 在第一个手指被移除后立即结束。我也无法使它完全按照您的要求进行。我会写子类,但我的注意力需要放在别处。祝你好运!

关于iphone - UILongPressGestureRecognizer - 只识别最后一次按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14008536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com