美文网首页
iOS 手势冲突

iOS 手势冲突

作者: _Waiting_ | 来源:发表于2023-02-14 22:11 被阅读0次

    一 : 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;
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS 手势冲突

          本文链接:https://www.haomeiwen.com/subject/nzcykdtx.html