farhan farhan - 2 months ago 18
Objective-C Question

How to detect a tab bar being double tapped?

Using NSNotification within Tabbar DidSelectItem like so to call a scroll up method.

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"homeScrollUp" object:nil];
}


It works nicely but how can i detect it by tapping index tab 0 twice for example?

Answer

How about something link this? Make sure you invalidate the timer when you're cleaning up the class.

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
   NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
   if(indexOfTab==0) {
      self.chosenIndexTapped++
   }
  self.tabPressTimer = [NSTimer scheduledTimerWithTimeInterval:smallTimeToAllowTap
                                                              target:self
                                                            selector:@selector(handleRecognisedTap)
                                                            userInfo:nil
                                                             repeats:NO];
  [[NSRunLoop currentRunLoop] addTimer:self.tabPressTimer forMode:NSRunLoopCommonModes]; 
}

-(void)handleRecognisedTap {
    if(self.chosenIndexTapped>1){
        //Scroll to top
    }
    self.chosenIndexTapped = 0 //Reset the taps on your chosen index
}