如果 UIGestureRecognizer 触发,如何取消按钮点击?
更新:问题似乎是对另一个 GestureRecognizer 的依赖失败。请参阅此问题下面的评论和测试项目!
在我的 iPhone 应用程序中,我有一个包含多个 UIButtons 作为子视图的视图。该视图还有一个 UITapGestureRecognizer,它正在监听两根手指的点击。
当在视图上发生两指点击时,我不希望按钮对点击做出反应,即使其中一根手指位于按钮内。我认为这就是“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];
Update: The problem seems to be the dependency on another GestureRecognizer to fail. See comments and test project below this question!
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.
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?
Edit: 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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
据苹果开发人员称,这是一个错误。我向 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!
Bug report:
UIGestureRecognizer 上有一种方法可以回答您的问题
基本上,您要求两次点击识别器在接受单击识别器之前失败。
所以,
There's a method on UIGestureRecognizer that respond to your question
Basically you're requiring that the two tap recognizer fail before accepting the single tap one.
So,
使用
delaysTouchesBegan
属性。将其设置为YES
。替代
禁用按钮上的用户交互,并像前面提到的那样附加单指点击识别器。在点击处理程序中,检查点击是否落在按钮的范围内。如果它在按钮的范围内,请执行
[theButton sendActionsForControlEvents:UIControlEventTouchUpInside];
。即使用户交互被禁用,这也会根据需要触发触摸事件。Use the
delaysTouchesBegan
property. Set it toYES
.Alternative
Disable user interaction on the buttons and attach a single finger tap recognizer like mentioned. In the tap handler, check if the tap falls within the bounds of the button. If it is within the bounds of a button, do
[theButton sendActionsForControlEvents:UIControlEventTouchUpInside];
. This will trigger the touch up event as desired even though the user interaction is disabled.马克,我遇到了同样的错误。如果你发布你的雷达#,我会在 bugreporter 中欺骗它。
我写了以下解决方法。我对 UITapGestureRecognizer 进行子类化,然后使用该子类来跟踪触发手势操作的触摸。如果我们在视图中的 TouchesEnded 中获得相同的触摸,我们将重定向到 TouchesCancelled。需要“immediateTarget”,因为视图上的touchesEnded 调用发生在手势的touchesEnded 之后但在调用手势的操作之前。
RPTapGestureRecognizer.h:
RPTapGestureRecognizer.m:
视图中:
Mark, I ran into the same bug. If you'll post your radar #, I'll dupe it in bugreporter.
I wrote the following workaround. I subclass UITapGestureRecognizer, then use the subclass to track the touches which triggered the gesture action. If we get the same touches in touchesEnded on the view, we redirect to touchesCancelled. 'immediateTarget' is needed because the touchesEnded call on the view comes after the gesture's touchesEnded but before the gesture's action is called.
RPTapGestureRecognizer.h:
RPTapGestureRecognizer.m:
In the view: