|
| 1 | +// |
| 2 | +// UITabBar+QMUI.m |
| 3 | +// qmui |
| 4 | +// |
| 5 | +// Created by MoLice on 2017/2/14. |
| 6 | +// Copyright © 2017年 QMUI Team. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "UITabBar+QMUI.h" |
| 10 | +#import "QMUICommonDefines.h" |
| 11 | +#import "UITabBarItem+QMUI.h" |
| 12 | + |
| 13 | +NSInteger const kLastTouchedTabBarItemIndexNone = -1; |
| 14 | + |
| 15 | +@interface UITabBar () |
| 16 | + |
| 17 | +@property(nonatomic, assign) BOOL canItemRespondDoubleTouch; |
| 18 | +@property(nonatomic, assign) NSInteger lastTouchedTabBarItemViewIndex; |
| 19 | +@property(nonatomic, assign) NSInteger tabBarItemViewTouchCount; |
| 20 | +@end |
| 21 | + |
| 22 | +@implementation UITabBar (QMUI) |
| 23 | + |
| 24 | ++ (void)load { |
| 25 | + static dispatch_once_t onceToken; |
| 26 | + dispatch_once(&onceToken, ^{ |
| 27 | + ReplaceMethod([self class], @selector(setItems:animated:), @selector(qmui_setItems:animated:)); |
| 28 | + ReplaceMethod([self class], @selector(setSelectedItem:), @selector(qmui_setSelectedItem:)); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +- (void)qmui_setItems:(NSArray<UITabBarItem *> *)items animated:(BOOL)animated { |
| 33 | + [self qmui_setItems:items animated:animated]; |
| 34 | + |
| 35 | + for (UITabBarItem *item in items) { |
| 36 | + if (!item.qmui_doubleTapBlock) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + UIControl *itemView = item.qmui_barButton; |
| 41 | + [itemView addTarget:self action:@selector(handleTabBarItemViewEvent:) forControlEvents:UIControlEventTouchUpInside]; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +- (void)qmui_setSelectedItem:(UITabBarItem *)selectedItem { |
| 46 | + NSInteger olderSelectedIndex = self.selectedItem ? [self.items indexOfObject:self.selectedItem] : -1; |
| 47 | + [self qmui_setSelectedItem:selectedItem]; |
| 48 | + NSInteger newerSelectedIndex = [self.items indexOfObject:selectedItem]; |
| 49 | + // 只有双击当前正在显示的界面的 tabBarItem,才能正常触发双击事件 |
| 50 | + self.canItemRespondDoubleTouch = olderSelectedIndex == newerSelectedIndex; |
| 51 | +} |
| 52 | + |
| 53 | +- (void)handleTabBarItemViewEvent:(UIControl *)itemView { |
| 54 | + |
| 55 | + if (!self.canItemRespondDoubleTouch) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // 如果一定时间后仍未触发双击,则废弃当前的点击状态 |
| 60 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 61 | + [self revertTabBarItemTouch]; |
| 62 | + }); |
| 63 | + |
| 64 | + NSInteger selectedIndex = [self.items indexOfObject:self.selectedItem]; |
| 65 | + |
| 66 | + if (self.lastTouchedTabBarItemViewIndex == kLastTouchedTabBarItemIndexNone) { |
| 67 | + // 记录第一次点击的 index |
| 68 | + self.lastTouchedTabBarItemViewIndex = selectedIndex; |
| 69 | + } else if (self.lastTouchedTabBarItemViewIndex != selectedIndex) { |
| 70 | + // 后续的点击如果与第一次点击的 index 不一致,则认为是重新开始一次新的点击 |
| 71 | + [self revertTabBarItemTouch]; |
| 72 | + self.lastTouchedTabBarItemViewIndex = selectedIndex; |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + self.tabBarItemViewTouchCount ++; |
| 77 | + if (self.tabBarItemViewTouchCount == 2) { |
| 78 | + // 第二次点击了相同的 tabBarItem,触发双击事件 |
| 79 | + UITabBarItem *item = self.items[selectedIndex]; |
| 80 | + if (item.qmui_doubleTapBlock) { |
| 81 | + item.qmui_doubleTapBlock(item, selectedIndex); |
| 82 | + } |
| 83 | + [self revertTabBarItemTouch]; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +- (void)revertTabBarItemTouch { |
| 88 | + self.lastTouchedTabBarItemViewIndex = kLastTouchedTabBarItemIndexNone; |
| 89 | + self.tabBarItemViewTouchCount = 0; |
| 90 | +} |
| 91 | + |
| 92 | +#pragma mark - Swizzle Property Getter/Setter |
| 93 | + |
| 94 | +static char kAssociatedObjectKey_canItemRespondDoubleTouch; |
| 95 | +- (void)setCanItemRespondDoubleTouch:(BOOL)canItemRespondDoubleTouch { |
| 96 | + objc_setAssociatedObject(self, &kAssociatedObjectKey_canItemRespondDoubleTouch, @(canItemRespondDoubleTouch), OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 97 | +} |
| 98 | + |
| 99 | +- (BOOL)canItemRespondDoubleTouch { |
| 100 | + return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_canItemRespondDoubleTouch)) boolValue]; |
| 101 | +} |
| 102 | + |
| 103 | +static char kAssociatedObjectKey_lastTouchedTabBarItemViewIndex; |
| 104 | +- (void)setLastTouchedTabBarItemViewIndex:(NSInteger)lastTouchedTabBarItemViewIndex { |
| 105 | + objc_setAssociatedObject(self, &kAssociatedObjectKey_lastTouchedTabBarItemViewIndex, @(lastTouchedTabBarItemViewIndex), OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 106 | +} |
| 107 | + |
| 108 | +- (NSInteger)lastTouchedTabBarItemViewIndex { |
| 109 | + return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_lastTouchedTabBarItemViewIndex)) integerValue]; |
| 110 | +} |
| 111 | + |
| 112 | +static char kAssociatedObjectKey_tabBarItemViewTouchCount; |
| 113 | +- (void)setTabBarItemViewTouchCount:(NSInteger)tabBarItemViewTouchCount { |
| 114 | + objc_setAssociatedObject(self, &kAssociatedObjectKey_tabBarItemViewTouchCount, @(tabBarItemViewTouchCount), OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 115 | +} |
| 116 | + |
| 117 | +- (NSInteger)tabBarItemViewTouchCount { |
| 118 | + return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_tabBarItemViewTouchCount)) integerValue]; |
| 119 | +} |
| 120 | + |
| 121 | +@end |
0 commit comments