天天看點

拖拽手勢和清掃手勢沖突時(UIPanGestureRecognizer和UISwipeGestureRecognizer沖突時)

  故事發生在這樣的情境上:給整個控制器添加了一個拖拽手勢,然後又在控制上的每個Cell上加了左滑清掃手勢,然後問題來了:隻有拖拽手勢起作用,而左滑手勢沒有效果了,然後怎麼解決這個問題呢!先上圖:

拖拽手勢和清掃手勢沖突時(UIPanGestureRecognizer和UISwipeGestureRecognizer沖突時)

當給整個控制器添加了拖拽手勢(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