(一)ios, 怎么关闭cell的点击, 使得cell 中的各view(1、2、3...) 响应点击事件
无非就是想响应cell中的子view1、view2的
- 尝试1 (行不通)
对cell中的view设置tap手势, addTaget:selector:, 但是偏偏就是走cell的didSelect方法, 不走tap的selector方法。
简单这样设置cell中子view手势,是没能达到 拦截cell的系统手势目的的
- 尝试2(ok), 2步
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时
-
<1.1>
tapCView和touchesBegan同时存在
结果: 只走touchesBegan方法, 往里面加[super touchesBegan:touches withEvent:event];也无济于事 -
<1.2>
tapCView存在, 注释掉touchesBegan
结果: 手势方法tapCView走起~
所以有<<UIView touch结论>>:
UIView中,touches优先于gesture; 同时存在时,gesture可能不走
注意的是: 有时候走完touches走gesture方法,可有时候又不走,用时谨慎
<2>.CView继承于UIScrollView时
-
<2.1>
tapCView和touchesBegan同时存在
结果: *只走tapCView方法 -
<2.2>
tapCView去掉 , 只有touchesBegan
结果:touchesBegan走起~
所以有<<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);
}
- <a.1> 点击tableView区, 只走
didSelect, 不走touchesBegan - <a.2> 点击非tableView区, 才走
touchesBegan
但是, 若给cview 添加 手势
- (void)tapCView:(UITapGestureRecognizer *)tap{
NSLog(@"%@ - %@",NSStringFromClass([self class]), self.gestureRecognizers);
}
- <a.3> 父cview有添加手势时, 只走手势方法, 子tableView的didSelect不走
此时<<UIView touch结论>> 有必要更改一下了:
- 1.如果
UIView没有子View时.touches优先于gesture,touches和gesture同时存在时,gesture手势方法 偶尔不执行- 2.如果
UIView有子View时.
- 2.1若点击的子view是
tableView, 只走父cview的gesture方法,touches不走了~ 注意~连自己的didSelect都不走了(厉害了).
估计90%的iOS开发者都遇到过吧,现象和solution👉看 博文1👉[iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效]- 2.2若点击的子view是
UIView,touches优先于gesture(父cview的gesture偶尔不执行)
其中, <<UIView touch结论>> 2.1 补充一下, 使用了博文1的解决方案, 在父cview中写了gesture代理方法,识别手势的touch.view是UITableViewCellContentView, 就够了; 若发现didSelect仍然无法执行, 去自定义tableView的里面去看看是否写了touches方法,删掉即可or增加[super touhes...]
我的理解是:
①通过遍历subView,hittest和pointInside方法确定在点击区的views;
②在views中找到 添加了gesture的view, 去响应
至于原因, 补习一下响应链
👉GitHub Demo - ZYTouchesHandle
👉推荐 iOS开发 - 事件传递响应链 (内含Demo哦~)
👉推荐 iOS触摸事件传递响应之被忽视的手势识别器工作原理
这篇也不错👉一篇搞定事件传递、响应者链条、hitTest和pointInside的使用









网友评论