更新:问题似乎是对另一个 GestureRecognizer 的依赖失败。请参阅此问题下方的评论和测试项目!
在我的 iPhone 应用程序中,我有一个将多个 UIButtons 作为 subview 的 View 。该 View 还有一个 UITapGestureRecognizer,它正在监听两根手指的敲击。
当在 View 上发生双指点击时,我不希望按钮对点击使用react,即使其中一根手指在按钮内。我认为这就是“cancelsTouchesInView”的用途,但这不起作用。
我现在的问题是:如何让我的按钮在识别到手势时忽略点击?
编辑:这是我的手势识别器。
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapped:)];
[doubleTap setNumberOfTouchesRequired:2];
[doubleTap setNumberOfTapsRequired:1];
[doubleTap setCancelsTouchesInView:YES];
[doubleTap setDelaysTouchesBegan:YES];
[doubleTap setDelaysTouchesEnded:YES];
[self.view addGestureRecognizer:doubleTap];
[doubleTap release];
最佳答案
根据 Apple 开发人员的说法,这是一个错误。我向 Apple 提交了错误报告。 非常感谢您的提示,Deepak 和 gcamp!
错误报告:
Summary: When adding two UITapGestureRecognizers to a view where one requires the other to fail (requiresGestureRecognizerToFail:) the cancelsTouchesInView property of the first gesture recognizer is ignored.
Steps to Reproduce: 1. Create two UITapGestureRecognizers (r1 and r2) 2. Configure r1 to require two touches and one tap and to delay touchesBegan 3. Configure r2 to require two touches and two taps and to delay touchesBegan 4. Configure r1 to require r2 to fail [r1 requiresGestureRecognizerToFail:r2] 5. Add r1 and r2 to a view 6. Place a UIButton in the view 7. Tap with two fingers on the view, one should hit the button.
Expected Results: r1 should be recognized and the button tap should be canceled (cancelsTouchesInView defaults to YES for UITapGestureRecognizers).
Actual Results: r1 is recognized but the button touchedUpInside event is fired, too.
Regression: cancelTouchesInView works fine for r1 once you remove the dependency on r2 (step 4).
关于ios - 如果 UIGestureRecognizer 触发,如何取消按钮点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188997/