具有 UITapGestureRecognizer 的视图内的 UIButton [英] UIButton inside a view that has a UITapGestureRecognizer
问题描述
我有一个UITapGestureRecognizer
的视图.因此,当我点击视图时,该视图上方会出现另一个视图.这个新视图有三个按钮.当我现在按下这些按钮之一时,我没有得到按钮动作,我只得到点击手势动作.所以我不能再使用这些按钮了.我该怎么做才能让事件通过这些按钮?奇怪的是按钮仍然突出显示.
I have view with a UITapGestureRecognizer
. So when I tap on the view another view appears above this view. This new view has three buttons. When I now press on one of these buttons I don't get the buttons action, I only get the tap gesture action. So I'm not able to use these buttons anymore. What can I do to get the events through to these buttons? The weird thing is that the buttons still get highlighted.
我不能在收到点击后就删除 UITapGestureRecognizer.因为有了它,新视图也可以被删除.意味着我想要像全屏视频控件一样的行为.
I can't just remove the UITapGestureRecognizer after I received it's tap. Because with it the new view can also be removed. Means I want a behavior like the fullscreen vide controls.
推荐答案
您可以将控制器或视图(以创建手势识别器为准)设置为 UITapGestureRecognizer
的代理.然后在委托中你可以实现 -gestureRecognizer:shouldReceiveTouch:
.在您的实现中,您可以测试触摸是否属于您的新子视图,如果是,则指示手势识别器忽略它.类似于以下内容:
You can set your controller or view (whichever creates the gesture recognizer) as the delegate of the UITapGestureRecognizer
. Then in the delegate you can implement -gestureRecognizer:shouldReceiveTouch:
. In your implementation you can test if the touch belongs to your new subview, and if it does, instruct the gesture recognizer to ignore it. Something like the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if (self.controlSubview.superview != nil) {
if ([touch.view isDescendantOfView:self.controlSubview]) {
// we touched our control surface
return NO; // ignore the touch
}
}
return YES; // handle the touch
}
这篇关于具有 UITapGestureRecognizer 的视图内的 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!