专注iOS开发

iOS各种手势冲突收集

2017-07-31  本文已影响279人  wustzhy
(一)ios, 怎么关闭cell的点击, 使得cell 中的各view(1、2、3...) 响应点击事件

无非就是想响应cell中的子view1、view2的

对cell中的view设置tap手势, addTaget:selector:, 但是偏偏就是走cell的didSelect方法, 不走tap的selector方法。
简单这样设置cell中子view手势,是没能达到 拦截cell的系统手势目的的
step1:对cell类中,将想要监听点击事件的子view1打开 , userInteractionEnabled = YES
step2:在cell类中,写touchesBegan方法,判断点击view的tag,或者classType,亦或是touchPoint区域
如果是你点击的view,那么写业务代码, over~。
这样就成功的拦截的cell的系统手势,不走didSelect方法了

看看 代码

  // cell 类中实现
  -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIImageView * imgV = (UIImageView *)touches.anyObject.view; // 开启userInteractionEnabled
    
    if ([self.delegate respondsToSelector:@selector(didTapHotRecommendCell:index:)]) {
        [self.delegate didTapHotRecommendCell: self
                                        index: (imgV.tag-ZYSubViewType_ImageView)/10
         ];
    }
    
    // [super touchesBegan:touches withEvent:event];  // 如果还想响应cell 的didSelect方法, 就写这句
  }

注意 attention 注意

[super touchesBegan:touches withEvent:event];
写了这句就表示, 继续下一个能响应者 接着走下个响应事件, cell的didSelect方法也会接着走了
如果你有特殊需求, 两个都要走, ok ~ 写上吧

(二) UIScrollView 手势之怪异
touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 
等只能被UIView捕获, 而UIScrollView不走touchesBegan~~~~

真相代码 如下


// CView类 继承于 UIScrollView or UIView  两种情况👇 ,两方法同存在

// 给cview的手势方法         //-UIScrollView走   //-UIView不走
- (void)tapCView:(UITapGestureRecognizer *)tap{
   NSLog(@"%@ - %@",NSStringFromClass([self class]), self.gestureRecognizers); 
}

// cview的touches方法      //-UIView走         //-UIScrollView不走
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@ - %@",NSStringFromClass([self class]), self.gestureRecognizers);
}

<1>.CView继承于UIView

所以有<<UIView touch结论>>: UIView中, touches优先于gesture; 同时存在时, gesture可能不走
注意的是: 有时候走完touchesgesture方法,可有时候又不走,用时谨慎

<2>.CView继承于UIScrollView

所以有<<UIScrollView touch结论>>: UIScrollView中, gesture优先于touches,与<<UIView touch结论>> 正好相反。
注意: 它很果决, 有gesture绝不走touches


(三) UITableView 手势之怪异

<a>接着上面的, 在CView类中添加子tableView , 前提CView: UIView

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@ - %@",NSStringFromClass([self class]), self.gestureRecognizers);
    [super touchesBegan:touches withEvent:event];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
}

但是, 若给cview 添加 手势

- (void)tapCView:(UITapGestureRecognizer *)tap{
   NSLog(@"%@ - %@",NSStringFromClass([self class]), self.gestureRecognizers); 
}

此时<<UIView touch结论>> 有必要更改一下了:

  • 1.如果UIView没有子View时. touches优先于gesture, touchesgesture同时存在时,gesture手势方法 偶尔不执行
  • 2.如果UIView有子View时.

其中, <<UIView touch结论>> 2.1 补充一下, 使用了博文1的解决方案, 在父cview中写了gesture代理方法,识别手势的touch.viewUITableViewCellContentView, 就够了; 若发现didSelect仍然无法执行, 去自定义tableView的里面去看看是否写了touches方法,删掉即可or增加[super touhes...]

    我的理解是: 
①通过遍历subView,hittest和pointInside方法确定在点击区的views; 
②在views中找到 添加了gesture的view, 去响应

至于原因, 补习一下响应链

👉GitHub Demo - ZYTouchesHandle
👉推荐 iOS开发 - 事件传递响应链 (内含Demo哦~)
👉推荐 iOS触摸事件传递响应之被忽视的手势识别器工作原理
这篇也不错👉一篇搞定事件传递、响应者链条、hitTest和pointInside的使用

上一篇 下一篇

猜你喜欢

热点阅读