拖拽手势和清扫手势冲突时(UIPanGestureRecognizer和UISwipeGestureRecognizer冲突时)

  故事发生在这样的情境上:给整个控制器添加了一个拖拽手势,然后又在控制上的每个Cell上加了左滑清扫手势,然后问题来了:只有拖拽手势起作用,而左滑手势没有效果了,然后怎么解决这个问题呢!先上图:

当给整个控制器添加了拖拽手势(UIPanGestureRecognizer),然后在控制器里面的UITableViewCell又添加了左滑清扫手势(UISwipeGestureRecognizer),造成了只有拖拽手势起了作用,而Cell的左滑手势已经不能滑动了!

解决办法就是给这两个手势设置一个优先级[panGes requireGestureRecognizerToFail:cell.leftSwipe];

关键代码

 1 + (instancetype)cellWithTableView:(UITableView *)tableView{
 2     static NSString *reuseIdentity = @"tanCell";
 3     
 4     TanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentity];
 5     
 6     if (cell == nil){
 7         cell = [[TanTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentity];
 8         
 9         //设置手势优先级,避免手势冲突
10         UIPanGestureRecognizer *panGes = [tableView.superview.gestureRecognizers objectAtIndex:0];
11         [panGes requireGestureRecognizerToFail:cell.leftSwipe];
12         [panGes requireGestureRecognizerToFail:cell.rightSwipe];
13     }
14     return cell;
15 }

至于如何给Cell设置左滑多菜单功能手势,见拙文:自定义UITableViewCell实现左滑动多菜单功能LeftSwipe

DEMO下载:

github: https://github.com/xiaotanit/Tan_SwipeAndPan

原文链接:http://www.cnblogs.com/tandaxia/p/5349090.html

原文地址:https://www.cnblogs.com/tandaxia/p/5349090.html