双击UITabBarItem以像Twitter应用程序一样向上滚动和重新加载

时间:2011-10-10 02:20:03

标签: ios uitabbaritem

如何在UITabBarItem上实现双击,以便它可以向上滚动UITableView,就像Twitter应用程序一样?或任何参考,将做

由于

3 个答案:

答案 0 :(得分:1)

您可以跟踪上次触摸日期并与当前触摸日期进行比较。 如果差异足够小(0.7秒),您可以认为它是双击。

使用委托方法UITabVarControllershouldSelectViewController的子类中实现此功能。

这是我正在使用的工作代码。

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

答案 1 :(得分:0)

您可以在标签栏上添加点按手势:

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

答案 2 :(得分:0)

您可以在UITabBarItem (QMUI)中看到QMUI iOS的代码,如果用户双击UITabBarItem,它支持使用阻止,您可以找到示例代码here

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