iOS 手势冲突

2023-02-14  本文已影响0人  _Waiting_

一 : view添加手势与tableView冲突问题

1. 遵守代理协议
    UIGestureRecognizerDelegate

    2. 给手势设置代理
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToTapGesture:)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];

    3.实现代理方法
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    // 点击的view的类名
    NSLog(@"%@", NSStringFromClass([touch.view class]));
    // 点击了tableViewCell,view的类名为UITableViewCellContentView,则不接收Touch点击事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

二 : view添加手势和collectionView的冲突问题

想要的效果 : 既触发didSelectItemAtIndexPath方法,又能在CollectionView自身上处理TapGesture

问题 : collectionView没有内容视图, 不能像上面那样解决冲突

解决方法 :

1. 遵守代理协议
UIGestureRecognizerDelegate

2. 给手势设置代理
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelDelete)]; 
apGestureRecognizer.delegate = self;  [注意点]
 [self.collectionview addGestureRecognizer:tapGestureRecognizer];

3. self要实现UIGestureRecognizerDelegate协议,其中就有gestureRecognizer:shouldReceiveTouch:方法 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != self.collectionView) {
        return NO;
    }else {
        return YES;
    }
}
上一篇下一篇

猜你喜欢

热点阅读