5

How do you implement a double tap on UITabBarItem so that it will scroll up the UITableView, like what the Twitter App does? Or any reference for this, will do

Thanks

Vincent Bacalso
  • 2,071
  • 4
  • 23
  • 34

3 Answers3

1

You can keep track of last touch date and compare to the current touch date. If the difference is small enough (0.7sec) you can consider it a double tap.

Implement this in a subclass of UITabVarController using a delegate method shouldSelectViewController.

Here is a working code that I am using.

#import "TabBarController.h"
#import "TargetVC.h"

@interface TabBarController ()

@property(nonatomic,retain)NSDate *lastTouchDate;

@end

@implementation TabBarController

//Remeber to setup UINavigationController of each of the tabs so that its restorationIdentifier is not nil
//You can setup the restoration identifier in the IB in the identity inspector for you UIViewController or your UINavigationController
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSString *tab = viewController.restorationIdentifier;

    if([tab isEqualToString:@"TargetVC"]){

        if(self.lastTouchDate){

            UINavigationController *navigationVC = (UINavigationController*)viewController;
            TargetVC *targetVC = (TargetVC*)navigationVC.viewControllers[0];

            NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:self.lastTouchDate];
            if(ti < 0.7f)
               [targetVC scrollToTop];

        }

        self.lastTouchDate = [NSDate date];
    }

    return YES;
}
Cyprian
  • 9,423
  • 4
  • 39
  • 73
0

You can add a tap gesture on the tabbar:

-(void)addTapGestureOnTabbar
{
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapTabbarHappend:)];
    tap.numberOfTapsRequired = 2;
    tap.delaysTouchesBegan = NO;
    tap.delaysTouchesEnded = NO;
    [_tabBarController.tabBar addGestureRecognizer:tap];
}

-(void)doubleTapTabbarHappend:(UITapGestureRecognizer *)gesture
{
    CGPoint pt = [gesture locationInView:self.tabBarController.tabBar];
    NSInteger count = self.tabBarController.tabBar.items.count;
    CGFloat itemWidth = [UIScreen mainScreen].bounds.size.width/(count*1.0);
    CGFloat temp =  pt.x/itemWidth;
    int index = floor(temp);
    if (index == kTabbarItemIndex) {
        //here to scroll up and reload 
    }
}
Kaibin
  • 474
  • 2
  • 8
  • 18
0

You can see the code of UITabBarItem (QMUI) in QMUI iOS, it supports to using a block if user double tap the UITabBarItem, and you can find a sample code here.

tabBarItem.qmui_doubleTapBlock = ^(UITabBarItem *tabBarItem, NSInteger index) {
    // do something you want...
};
MoLice
  • 301
  • 2
  • 7