tomiQrsd
tomiQrsd

Reputation: 119

Disable switching tabs after long press on UITabBarItem

I want to disable UITabBarViewController's ability to switch to long pressed UITabBarItem at specific tag.

What I tried is

  1. subclassed UITabBarViewControlleras UIGestureRecognizerDelegate
  2. added UILongPressGestureRecognizer and set it's delegate to self
  3. overriden gestureRecognizerShouldBegin and made it to return NO

But it didn't work.

Mind you that I already have UITapGestureRecognizer *recognizer added to one of the UITabBarItem like this:

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

And it works fine. I would love to just disable recognizing long pressing and fire UITapGestureRecognizer instantly, even while long pressing.

Thanks

Upvotes: 0

Views: 556

Answers (1)

vivek
vivek

Reputation: 459

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

and in the handle long press method

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

Make your AppDelegate a UITabBarControllerDelegate, in didFinishLaunchingWithOptions: call

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

and add this method:

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

Upvotes: 1

Related Questions