UIButton 触摸事件落入底层视图
我创建了一个小的 UIView,其中包含两个 UIButton。该视图响应 UITapGesture
事件。按钮应该响应 TouchUpInside,但是当我点击按钮时,响应者是底层视图,并且点击手势选择器被触发。寻求意见或建议。
I have created a small UIView
which contains two UIButton
s. The view responds to UITapGesture
events. The Buttons are supposed to respond to TouchUpInside
, however when I tap the buttons the responder is the underlying view and the tap gesture selector is triggered. Looking for advice or suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以修改橙色视图中响应点击手势的方法:
这样,如果您触摸 UIButton,点击将被底层视图忽略。
You can modify the method that responds to the tap gesture in the orange view:
This way if you touch a UIButton, the tap will be ignored by the underlying view.
正确的答案(它可以防止 tabrecognizer 劫持任何点击,并且不需要您实现委托等)是 在这里找到。我通过这篇文章找到了这个答案。
简而言之:
“这可以防止点击识别器成为唯一一个捕获所有
水龙头”
The right answer (which prevents the tabrecognizer from highjacking any taps and doesn't need you to implement a delegate etc) is found here. I got a lead to this answer via this post.
In short use:
"Which prevents the tap recognizer to be the only one to catch all the
taps"
每个 UIView 都有一个“exclusiveTouch”属性。如果设置为 YES,视图将不会将触摸传递到响应者链。尝试在 UIButton 上设置此属性,看看是否会产生影响。
Each UIView has an 'exclusiveTouch' property. If it's set to YES the view won't pass the touch down the responder chain. Try setting this property on your UIButtons and see if that makes a difference.
视图在视图层次结构中是如何排序的?另外,你是在IB中创建接口吗?如果您正确连接连接,这根本不应该是问题......
How are the views ordered in the view hierarchy? Also, are you creating the interface in IB? If you hook up the connections properly, this shouldn't be an issue at all...
这种设计的问题在于它类似于将一个按钮嵌入到另一个按钮中。虽然您可能有充分的理由这样做,但您应该更加小心事件流。
发生的情况是手势识别器在触摸到达子视图(您的例子中的两个按钮)之前拦截触摸。您应该实现 UIGestureRecognizerDelegate 协议,即
gestureRecognizer:shouldReceiveTouch:
方法,如果触摸位于两个按钮中的任何一个内部,则返回 NO。这将防止橙色视图篡夺按钮的触摸,并且按钮将开始按预期工作。The problem with this design is that it's similar to embedding one button into another. While you may have a valid reason to do it, you should be more careful with the event flow.
What happens is the gesture recognizer intercepting touches before they reach subviews (the two buttons in your case). You should implement UIGestureRecognizerDelegate protocol, namely,
gestureRecognizer:shouldReceiveTouch:
method, and return NO if the touch is inside any of the two buttons. That will prevent the orange view from usurping touches intended for the buttons and the buttons will start to work as expected.看看这个问题。
uibutton-inside-a-view-that-has-a-uitapgesturerecognizer< /a>
Check out this question.
uibutton-inside-a-view-that-has-a-uitapgesturerecognizer