从UITapGestureRecognizer中排除子视图 [英] exclude subview from UITapGestureRecognizer
问题描述
我有一个子视图和一个superview。超级视图有一个UITapGestureRecognizer连接到它。
I have a subview and a superview. The superview has an UITapGestureRecognizer attached to it.
UIView *superview = [[UIView alloc] initWithFrame:CGRectMake:(0, 0, 320, 480);
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake:(100, 100, 100, 100);
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap);
superview.userInteractionEnabled = YES;
subview.userInteractionEnabled = NO;
[superview addGestureRecognizer:recognizer];
[self addSubview:superview];
[superview addSubview:subview];
识别器也在子视图内被触发,是否有一种方法从子视图中排除识别器?
The recognizer is fired inside the subview as well, is there a way to exclude the recognizer from the subview?
我知道此问题之前已被问过,但我没有找到一个好的答案。
I know this question has been asked before but I didn't find a good answer to it.
推荐答案
您可以使用手势识别代理来限制可以识别触摸的区域,类似于此示例:
You can use gesture recognizer delegate to limit area where it can recognise touches similar to this example:
recognizer.delegate = self;
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint touchPoint = [touch locationInView:superview];
return !CGRectContainsPoint(subview.frame, touchPoint);
}
请注意,您需要保持对父视图和子视图的引用实例变量?),以便能够在委托方法
Note that you need to keep reference to your parent and child view (make them instance variables?) to be able to use them in delegate method
这篇关于从UITapGestureRecognizer中排除子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!