UIGestureRecognizer 阻止子视图处理触摸事件 [英] UIGestureRecognizer blocks subview for handling touch events
问题描述
我正在尝试弄清楚这是如何以正确的方式完成的.我试图描述这种情况:
I'm trying to figure out how this is done the right way. I've tried to depict the situation:
我正在添加一个 UITableView
作为 UIView
的子视图.UIView
响应点击和 pinchGestureRecognizer
,但是当这样做时,tableview 停止对这两个手势做出反应(它仍然对滑动做出反应).
I'm adding a UITableView
as a subview of a UIView
. The UIView
responds to a tap- and pinchGestureRecognizer
, but when doing so, the tableview stops reacting to those two gestures (it still reacts to swipes).
我已经用下面的代码让它工作了,但这显然不是一个很好的解决方案,我相信有更好的方法.这是放在 UIView
(超级视图)中:
I've made it work with the following code, but it's obviously not a nice solution and I'm sure there is a better way. This is put in the UIView
(the superview):
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if([super hitTest:point withEvent:event] == self) {
for (id gesture in self.gestureRecognizers) {
[gesture setEnabled:YES];
}
return self;
}
for (id gesture in self.gestureRecognizers) {
[gesture setEnabled:NO];
}
return [self.subviews lastObject];
}
推荐答案
我遇到了一个非常相似的问题并找到了我的解决方案 在这个问题中.总之,将自己设置为 UIGestureRecognizer
的代理,然后在允许识别器处理触摸之前检查目标视图.相关的委托方法是:
I had a very similar problem and found my solution in this SO question. In summary, set yourself as the delegate for your UIGestureRecognizer
and then check the targeted view before allowing your recognizer to process the touch. The relevant delegate method is:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
这篇关于UIGestureRecognizer 阻止子视图处理触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!