我有一个子视图和一个超级视图. superview附加了一个UITapGestureRecognizer.
UIView *superview = [[UIView alloc] initWithFrame:CGRectMake:(0,320,480); UIView *subview = [[UIView alloc] initWithFrame:CGRectMake:(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];
识别器也会在子视图中触发,有没有办法从子视图中排除识别器?
我知道之前已经问过这个问题,但我没有找到一个好的答案.
解决方法
您可以使用手势识别器委托来限制可以识别触摸的区域,类似于此示例:
recognizer.delegate = self; ... - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ CGPoint touchPoint = [touch locationInView:superview]; return !CGRectContainsPoint(subview.frame,touchPoint); }
请注意,您需要保持对父视图和子视图的引用(使它们成为实例变量?)才能在委托方法中使用它们
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。