Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Tap on UITabBarItem to Scroll up and Reload like Twitter App

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

like image 624
Vincent Bacalso Avatar asked Nov 13 '22 16:11

Vincent Bacalso


1 Answers

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;
}
like image 109
Cyprian Avatar answered Jan 01 '23 07:01

Cyprian