故事發生在這樣的情境上:給整個控制器添加了一個拖拽手勢,而後又在控制上的每一個Cell上加了左滑清掃手勢,而後問題來了:只有拖拽手勢起做用,而左滑手勢沒有效果了,而後怎麼解決這個問題呢!先上圖:html
當給整個控制器添加了拖拽手勢(UIPanGestureRecognizer),而後在控制器裏面的UITableViewCell又添加了左滑清掃手勢(UISwipeGestureRecognizer),形成了只有拖拽手勢起了做用,而Cell的左滑手勢已經不能滑動了!git
解決辦法就是給這兩個手勢設置一個優先級:[panGes requireGestureRecognizerToFail:cell.leftSwipe];github
關鍵代碼ui
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實現左滑動多菜單功能LeftSwipespa
DEMO下載:code
github: https://github.com/xiaotanit/Tan_SwipeAndPanhtm