案例說明:點擊下圖子控件,會觸發(fā)父控件的點擊事件
解決方法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:控制點擊區(qū)域
使用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{
// 相當于點擊了父控件
};
}];
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者