比如下图,我给Controller的View加上了一个手势UITapGestureRecognizer,但我并不希望它的子控件(雪青色的View)也响应我父控件的点击事件.
首先,给这个手势设置代理
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:tap];
tap.numberOfTapsRequired = 1;
tap.delegate = self;
实现代理代理方法,在代理方法里判断即可
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
if([touch.view isDescendantOfView:self.mySubView]) {
return NO;
}
return YES;
}