长按 UITabBarItem 后禁用切换选项卡

Disable switching tabs after long press on UITabBarItem

我想禁用 UITabBarViewController 在特定 tag 时切换到长按 UITabBarItem 的功能。

我试过的是

  1. 子类UITabBarViewControllerUIGestureRecognizerDelegate
  2. 添加了 UILongPressGestureRecognizer 并将其 delegate 设置为 self
  3. 覆盖 gestureRecognizerShouldBegin 并进入 return NO

但是没有用。

请注意,我已经将 UITapGestureRecognizer *recognizer 添加到 UITabBarItem 之一,如下所示:

[self.tabBar.subviews[2] addGestureRecognizer:recognizer]

而且效果很好。我很想禁用识别长按并立即触发 UITapGestureRecognizer,即使在长按时也是如此。

谢谢

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 0.5;
        [self addGestureRecognizer:longPress];

和手柄长按方法

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
      NSLog(@"UIGestureRecognizerStateEnded");
    //Do Whatever You want on End of Gesture
[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:FALSE];
     }
    else if (sender.state == UIGestureRecognizerStateBegan){
       NSLog(@"UIGestureRecognizerStateBegan.");
   //Do Whatever You want on Began of Gesture
     }
  }

让你的 AppDelegate 成为 UITabBarControllerDelegate,在 didFinishLaunchingWithOptions: 调用

   UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    tabBarController.delegate = (id)self;

并添加此方法:

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; 
{    
       if (viewController.restorationIdentifier isEqualToString:@"foo")
           return YES;
       else
           return NO;
}