拖拽手勢和清掃手勢沖突時(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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM