UITabBarItem 双击事件

2022-06-13  本文已影响0人  championfu

业务需求:在选中某一Tab后,为其添加单击和双击事件(注意单击双击事件必须需独立,不能同时触发)

方案构思:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    ///第二个item是你要处理单双击的情况
    ///tapTimestamp用于记录上一次点击的时间戳。
    ///doubleTapInterval是一个静态常量(我设置的为0.3)
    NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
    if (index == 1 && tabBarController.selectedIndex == 1) {
        NSTimeInterval timestamp = CFAbsoluteTimeGetCurrent();
        if (timestamp - self.tapTimestamp < doubleTapInterval) {
            [self postDoubleTapNotification];
        }else {
            [self performSelector:@selector(postSingleTapNotification) afterDelay:doubleTapInterval];
        }
        self.tapTimestamp = timestamp;
        return NO;
    }
    return YES;
}

- (void)postSingleTapNotification {
    [NotiCenter postNotificationName:LSTabBarSingleTapNotification object:nil];
}

- (void)postDoubleTapNotification {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(postSingleTapNotification) object:nil];
    [NotiCenter postNotificationName:LSTabBarDoubleTapNotification object:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读