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

发布于 2024-12-08 17:33:15 字数 89 浏览 2 评论 0原文

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-12-15 17:33:15

您可以跟踪上次触摸日期并与当前触摸日期进行比较。
如果差异足够小(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;
}

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;
}
咽泪装欢 2024-12-15 17:33:15

您可以在选项卡栏上添加点击手势:

-(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 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 
    }
}
皓月长歌 2024-12-15 17:33:15

可以看到 UITabBarItem (QMUI) 的代码QMUI iOS,如果用户双击UITabBarItem,它支持使用块,您可以找到示例代码这里

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

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...
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文