【问题标题】:Double Tap on UITabBarItem to Scroll up and Reload like Twitter App双击 UITabBarItem 以向上滚动并像 Twitter 应用程序一样重新加载
【发布时间】:2011-10-10 02:20:03
【问题描述】:

如何实现双击 UITabBarItem 以便它向上滚动 UITableView,就像 Twitter 应用程序所做的那样?或者任何对此的参考,都可以

谢谢

【问题讨论】:

  • 这是不鼓励的吗?

标签: ios uitabbaritem


【解决方案1】:

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

使用委托方法shouldSelectViewControllerUITabVarController 的子类中实现这一点。

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      QMUI iOS中可以看到UITabBarItem (QMUI)的代码,支持双击UITabBarItem使用block,可以找到示例代码here

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-04
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 1970-01-01
        • 2013-12-14
        • 2016-10-25
        相关资源
        最近更新 更多