iOS开发中让子视图不响应父视图的手势识别器

一.问题描述

给父视图上添加一个子视图,给父视图添加一个手势识别器UITapGestureRecognizer,点击子视图时,也会触发UITapGestureRecognizer所关联的事件

代码:

UIView *blackV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HIGHT)];
blackV.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.7];
self.blackV = blackV;

[self.view addSubview:blackV];

DeviceListView *listV = [[DeviceListView alloc]initWithFrame:CGRectZero];
listV.deviceDelegate = self;
[blackV addSubview:listV];
[listV mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(blackV);
make.height.offset(200);
}];
self.listV = listV;

UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent)];
singleFingerOne.numberOfTouchesRequired = 1; //手指数
singleFingerOne.numberOfTapsRequired = 1; //tap次数
singleFingerOne.delegate = self;
[blackV addGestureRecognizer:singleFingerOne];

二.解决方法
使用UIGestureRecognizerDelegate的代理方法-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch,将子视图的tap手势屏蔽

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

if ([touch.view isDescendantOfView:self.listV]) {
return NO;
}

return YES;

}