tomiQrsd tomiQrsd - 1 month ago 5
iOS Question

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
    UITabBarViewController
    as
    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

Answer
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 2.0;
        [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
     }
  }


 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; 
{    
       if (tabBarController.selectedIndex == 0) {
            if(isUserLoggedIn)
                return YES;
            else
                return NO;
        }

        return YES; 
}
Comments