长按UITabBarItem后禁用切换选项卡

时间:2016-10-19 18:45:12

标签: ios objective-c uitabbarcontroller uigesturerecognizer

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

我尝试的是

  1. UITabBarViewController UIGestureRecognizerDelegate归为UILongPressGestureRecognizer
  2. 添加delegate并将其self设置为gestureRecognizerShouldBegin
  3. 覆盖NO并将其返回UITapGestureRecognizer *recognizer
  4. 但它没有用。

    请注意,我已将UITabBarItem添加到[self.tabBar.subviews[2] addGestureRecognizer:recognizer] 之一,如下所示:

    UITapGestureRecognizer

    它工作正常。即使长时间按下,我也希望立即禁用识别长按并立即触发dataset

    由于

1 个答案:

答案 0 :(得分:1)

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;
}