开发者

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

开发者 https://www.devze.com 2023-04-12 09:03 出处:网络
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开发者_Go百科 this, will do

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开发者_Go百科 this, will do

Thanks


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;
}


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 
    }
}


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...
};
0

精彩评论

暂无评论...
验证码 换一张
取 消