子视图响应父视图的点击事件

点击子视图却依然会响应父试图的点击事件发现UITapGestureRecognizer上的一个代理方法:

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

如果返回no则不响应,因此我们可以根据isDescendantOfView:方法来判定一个视图是否在其父视图的视图层中。把我们不想让其响应的子视图拿出来判断一下,如果包含这个子视图就返回no,否则就是yes。

UIGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    tapper.delegate = self;
    [self.view addGestureRecognizer:tapper];

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.inputView]) {
        return NO;
    }
    return YES;
}

你可能感兴趣的:(子视图响应父视图的点击事件)