防止TableView 上的tap手势隔断 cell的选择

时间:2024-01-14 21:56:02
遵循UIGestureRecognizerDelegate协议:

1.0添加手势
- (void)addTapGest {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    tap.delegate = self;
    [self.tableView addGestureRecognizer:tap];
}

2.拦截部分tap手势
#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    //防止拦截cell上的点击
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }

    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UILabel"]) {
        return NO;
    }
    return  YES;
}