问题描述
更新:问题似乎是依赖另一个GestureRecognizer失败。请参阅此问题下面的评论和测试项目!
在我的iPhone应用程序中,我有一个包含多个UIButton作为子视图的视图。该视图还有一个UITapGestureRecognizer,可以用两根手指敲击。
In my iPhone app I have a view with multiple UIButtons as subviews. The view also has a UITapGestureRecognizer which is listening for taps with two fingers.
当在视图上发生双指敲击时,我不希望按钮做出反应即使其中一个手指在按钮内,也可以轻敲。我认为这是cancelsTouchesInView的用途,但这不起作用。
When a two-finger-tap occurs on the view I don't want the buttons to react to the tap, even if one of the fingers was inside the button. I thought this is what "cancelsTouchesInView" is for, but that doesn't work.
我现在的问题是:当手势是如何告诉我的按钮忽略点击识别?
My question now is: How to tell my buttons to ignore taps when a gesture is recognized?
编辑:这是我的手势识别器。
This is my gesture recognizer.
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!
According to an Apple dev this is a bug. I filed a bug report with Apple.Thanks a lot for your hints, Deepak and gcamp!
错误报告:
重现步骤:
1.创建两个UITapGestureRecognizers(r1和r2)
2.将r1配置为需要两次触摸和一次点击并延迟touchesBegan
3.配置r2需要两次触摸和两次点击并延迟touchesBegan
4.配置r1以要求r2失败[r1 requiresGestureRecognizerToFail:r2]
5.将r1和r2添加到视图
6.放置视图中的UIButton
7.在视图上点击两个手指,一个应按下按钮。
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.
预期结果:
r1应为识别并取消按钮点击(对于UITapGestureReco,cancelsTouchesInView默认为YES gnizers)。
Expected Results: r1 should be recognized and the button tap should be canceled (cancelsTouchesInView defaults to YES for UITapGestureRecognizers).
实际结果:
r1被识别,但按钮touchUpInside事件也被触发。
Actual Results: r1 is recognized but the button touchedUpInside event is fired, too.
回归:
一旦你删除了对r2的依赖关系,对于r1,cancelTouchesInView就可以了。(/ p>
Regression: cancelTouchesInView works fine for r1 once you remove the dependency on r2 (step 4).
这篇关于如果UIGestureRecognizer触发,如何取消按钮点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!