4

我使用標籤欄上的長按手勢。但我只需要一個特定的標籤欄項目的長按手勢。在單個UITabBarItem上處理長按

我該如何解決這個問題?我可以自定義標籤欄中的長按手勢嗎?

+0

請分享您的代碼或IB。 –

+0

我沒有開始正確的代碼。 –

回答

5

您可以繼承UITabBarController並將它添加到UILongPressGestureRecognizer它的tabBar。作爲手勢識別器的代表,您可以選擇何時檢測長按。由於一旦用戶觸摸它,標籤欄項目就會被選中,您可以使用selectedItem屬性來執行此檢查。

@interface TabBarController() <UIGestureRecognizerDelegate> 
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressRecognizer; 

@end 

@implementation TabBarController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerFired:)]; 
    self.longPressRecognizer.delegate = self; 
    [self.tabBar addGestureRecognizer:self.longPressRecognizer]; 
} 

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { 

    // This will ensure the long press only occurs for the 
    // tab bar item which has it's tag set to 1. 
    // You can set this in Interface Builder or in code 
    // wherever you are creating your tabs. 
    if (self.tabBar.selectedItem.tag == 1) { 
     return YES; 
    } 
    else { 
     return NO; 
    } 

} 

- (void)recognizerFired:(UILongPressGestureRecognizer *)recognizer { 
    // Handle the long press... 
} 

@end 
+2

是否可以識別長按TabBarItem而不切換選項卡? –

1

如果你只需要認識上的TabBar項目之一的長按,就可以在相應的viewController的viewDidLoad方法做到這一點:

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)]; 
[self.tabBarController.tabBar addGestureRecognizer: longPressGesture]; 

然後:

- (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer { 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 

     UITabBar *tabBar = ((UITabBar*)recognizer.view); 

     if (tabBar.selectedItem == self.tabBarItem) { 
      doSomethingVeryExciting(); 
     } 
    } 
} 

如果你只是切換標籤,這不會觸發。

2

以下是我做的雨燕採用3:

protocol MyTabControllerProtocol: class { 
    func tabLongPressed() 
} 

class MyTabController: UITabBarController { 
    func viewDidLoad() { 
     super.viewDidLoad() 

     viewControllers = [ 
      // add your view controllers for each tab bar item 
      // NOTE: if you want view controller to respond to long press, then it should extend MyTabControllerProtocol 
     ] 

     let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(astroButtonItemLongPressed(_:))) 
     tabBar.addGestureRecognizer(longPressRecognizer) 
    } 

    func astroButtonItemLongPressed(_ recognizer: UILongPressGestureRecognizer) { 
     guard recognizer.state == .began else { return } 
     guard let tabBar = recognizer.view as? UITabBar else { return } 
     guard let tabBarItems = tabBar.items else { return } 
     guard let viewControllers = viewControllers else { return } 
     guard tabBarItems.count == viewControllers.count else { return } 

     let loc = recognizer.location(in: tabBar) 

     for (index, item) in tabBarItems.enumerated() { 
      guard let view = item.value(forKey: "view") as? UIView else { continue } 
      guard view.frame.contains(loc) else { continue } 

      if let nc = viewControllers[index] as? UINavigationController { 
       if let vc = nc.viewControllers.first as? MyTabControllerProtocol { 
        vc.tabLongPressed() 
       } 
      } else if let vc = viewControllers[index] as? MyTabControllerProtocol { 
       vc.tabLongPressed() 
      } 

      break 
     } 
    } 
} 
相關問題