2014-08-27 147 views
4

我的手勢識別器有問題。我的目標是實現在我的表格視圖中使用滑動刪除。但我認爲其他手勢彼此衝突。我正在使用這個庫romaonthego/REFrostedViewController 這是我的漢堡菜單的一個庫,這個庫有一個pangesture功能。我認爲衝突是在手勢中。因爲當我在另一個項目中運行我的tableview代碼時,它正在工作。請幫助,謝謝您。手勢衝突UITableView滑動刪除iOS

+0

我認爲...你必須禁用「panGestureEnabled」並手動呈現「菜單」[self.frostedViewController presentMenuViewController]; – TonyMkenu 2014-08-27 11:49:55

+0

但我也需要平移手勢。我禁用了平移手勢,但它根本不工作。我看到一些應用程序在那裏使用平移手勢菜單和手勢來刪除表中的數據。我想知道他們是如何管理兩輪2平移手勢。 T_T – 2014-08-27 11:53:39

+0

但是..你有什麼衝突?您無法呈現菜單......或者您無法滑動單元格? – TonyMkenu 2014-08-27 12:54:57

回答

0

首先...看看,如果你有這個

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

和第二...

嘗試添加該

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 

    if ([gestureRecognizer.view isKindOfClass:[UITableView class]]) { 
     return YES; 
    } else { 
     return NO; 
    } 
} 

https://stackoverflow.com/a/14338043

+0

那麼...你有什麼消息嗎? – TonyMkenu 2014-09-04 10:00:27

+0

對不起,我已經通過參考漢堡菜單的手勢來解決我的問題。謝謝你的回答:) – 2014-10-09 01:44:59

+0

@AlvinJohnTandoc我在當前的項目中遇到類似的問題。我假設TonyMkenu提供的解決方案爲您工作。如果我的假設是正確的,你能否告訴我該把代碼放在哪裏(功能在答案中提供)?現在我的代碼不會在任何地方使用這些函數。那麼我應該把它放在菜單類還是我自己的視圖控制器類中? – 2015-05-29 12:47:44

3

我有一個類似的問題,我最終做的和TonyMkenu類似,但是有更多的認識者認爲y OU需要允許:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 

if (otherGestureRecognizer.delegate == self) 
    return NO; 

//if otherGestureRecognizer is swipe to delete from a UITableView cancel slide menu recognizers 

if ([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableView class]]) 
{ 
    NSLog(@"Allow1 %@", [otherGestureRecognizer description]); 

    return YES; 
} 

if([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableViewCell class]] || 
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewCellScrollView"] || 
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewWrapperView"]) 
{ 
    NSLog(@"Allow&Disable %@", [otherGestureRecognizer description]); 

    if(gestureRecognizer.delegate == self) 
    {//cancel the slide menu recognizer 

     gestureRecognizer.enabled = NO; 
     gestureRecognizer.enabled = YES; 
    } 

    return YES; 
} 

NSLog(@"Deny %@", [otherGestureRecognizer description]); 
return NO; 

}

2

編輯:更新了iOS的11

其他的答案是有幫助的,但對我來說最好的解決辦法是應該做的邏輯shouldRequireFailureOfOtherGesture像所以:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    if (gestureRecognizer == self.pan) { 
     return YES; 
    } 
    return NO; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    if (gestureRecognizer == self.pan) { 

     // iOS 10 
     if ([NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewWrapperView"]) { 
      return YES; 
     } 
     // iOS 11 
     else if ([otherGestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UITableView class]]) { 
      return YES; 
     } 
    } 
    return NO; 
} 

這在我的情況下有更好的行爲。我還在我的平底鍋手勢上使用了delaysTouchesBegan = YES。可能有用!

+0

UITableViewWrapperView未在iOS 11中使用。任何人都可以在iOS 11中使用它? – thejaz 2017-10-21 14:12:04

+0

@thejaz我更新了我的答案iOS 11,希望有幫助! – beebcon 2017-10-25 14:26:13

0

在iOS 11中,希望這可以幫助你。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 
    if ([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableView class]]) { 
     if ([otherGestureRecognizer isKindOfClass: [UIPanGestureRecognizer class]]) { 
      UIPanGestureRecognizer *otherPan = (UIPanGestureRecognizer *)otherGestureRecognizer; 
      CGPoint translation = [otherPan translationInView:otherGestureRecognizer.view]; 
      return translation.x < 0; 
     } 
    } 
    return NO; 
}