案例说明:点击下图子控件,会触发父控件的点击事件
解决方法1:
给子控件添加空事件、或者子控件实例为UIButton,添加空事件
解决方法2:事件拦截
tap.delegate = self;
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// self.contentView为子控件
if ([touch.view isDescendantOfView:self.contentView]) {
return NO;
}
return YES;
}
解决方法3:控制点击区域
使用RAC手势为例
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
tap.delegate = self;
[self addGestureRecognizer:tap];
@weakify(self);
[[tap rac_gestureSignal] subscribeNext:^(UITapGestureRecognizer *tap) {
@strongify(self);
if(CGRectContainsPoint(self.frame, [tap locationInView:self.contentView])) {
// 相当于点击了子控件
} else{
// 相当于点击了父控件
};
}];