Search code examples
iosobjective-cuitabbarcontrolleruitabbaritem

Can I handle the double tap in UITabbarController


I need to handle the double tap action to get back from a navigation path represented with a custom view.

Usually the double tap dismisses the nested topmost controller in the navigation controller's stack. I'd like to handle this action and do something else.

Placing code in (BOOL)tabBarController:shouldSelectViewController: does not help as there is no difference between the single and double tap.

Thanks.


Solution

  • I had to add a tapCounter in the tabBarController:shouldSelectViewController: implementation:

    self.tapCounter++;
    
    
    // rule out possibility when the user taps on two different tabs very fast
    BOOL hasTappedTwiceOnOneTab = NO;
    
    if(self.previousHandledViewController == viewController) {
    
        hasTappedTwiceOnOneTab = YES;
    }
    
    self.previousHandledViewController = viewController;
    
    
    // this code is called in the case when the user tapped twice faster then tapTimeRange
    CGFloat tapTimeRange = 0.3;
    
    
    
            if(self.tapCounter == 2 && hasTappedTwiceOnOneTab) {
    
    
                // do something when tapped twice 
    
                self.tapCounter = 0;
                return NO; // or YES when you want the default engine process the event
    
            } else if(self.tapCounter == 1) {
    
                __block BOOL isSameViewControllerSelected = self.selectedViewController == viewController;
                if(isSameViewControllerSelected) {
                    // do something when tapped once
                }
    
                dispatch_after_delay_on_main_queue(tapTimeRange, ^{
    
                    self.tapCounter = 0; // reset the counter in case there is a single tap followed with another one, but with longer time then tapTimeRange
                });
    
                return NO; // or YES when you want the default engine process the event 
            }
    

    This works nicely without any private api calls. But I'd like to know if there is a better and a less complicated way.