首页 > 代码库 > iOS 子视图响应父视图的点击事件
iOS 子视图响应父视图的点击事件
最近为了解决 点击子视图却依然会响应父试图的点击事件发现UITapGestureRecognizer上的一个代理方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
如果返回no则不响应,因此我们可以根据isDescendantOfView:方法来判定一个视图是否在其父视图的视图层中。把我们不想让其响应的子视图拿出来判断一下,如果包含这个子视图就返回no,否则就是yes。
具体写法:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(removeSubviews)];
tap.delegate = self;
self.userInteractionEnabled = YES;
[self.alpha_back_view addGestureRecognizer:tap];
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isDescendantOfView:self.tableView_back_view]) {
return NO;
}
return YES;
}
iOS 子视图响应父视图的点击事件