【发布时间】:2011-10-10 02:20:03
【问题描述】:
如何实现双击 UITabBarItem 以便它向上滚动 UITableView,就像 Twitter 应用程序所做的那样?或者任何对此的参考,都可以
谢谢
【问题讨论】:
-
这是不鼓励的吗?
标签: ios uitabbaritem
如何实现双击 UITabBarItem 以便它向上滚动 UITableView,就像 Twitter 应用程序所做的那样?或者任何对此的参考,都可以
谢谢
【问题讨论】:
标签: ios uitabbaritem
您可以跟踪上次触摸日期并与当前触摸日期进行比较。 如果差异足够小(0.7 秒),您可以将其视为双击。
使用委托方法shouldSelectViewController 在UITabVarController 的子类中实现这一点。
这是我正在使用的工作代码。
#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;
}
【讨论】:
您可以在标签栏上添加点击手势:
-(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
}
}
【讨论】:
QMUI iOS中可以看到UITabBarItem (QMUI)的代码,支持双击UITabBarItem使用block,可以找到示例代码here。
tabBarItem.qmui_doubleTapBlock = ^(UITabBarItem *tabBarItem, NSInteger index) {
// do something you want...
};
【讨论】: