通常, 我們會采用如下的一些措施來防止重復(fù)點(diǎn)擊UIButton:
使用UIButton的enabled或userInteractionEnabled
使用UIButton的enabled屬性, 在點(diǎn)擊后, 禁止UIButton的交互, 直到完成指定任務(wù)之后再將其enable即可.
[btn addTarget:self action:@selector(actionFixMultiClick_enabled:) forControlEvents:UIControlEventTouchUpInside];
// xxx
- (void)actionFixMultiClick_enabled:(UIButton *)sender {
sender.enabled = NO;
[self btnClickedOperations];
}
- (void)btnClickedOperations {
self.view.backgroundColor = [UIColor colorWithRed:((arc4random() % 255) / 255.0)
green:((arc4random() % 255) / 255.0)
blue:((arc4random() % 255) / 255.0)
alpha:1.0f];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"btnClickedOperations");
btn.enabled = YES;
});
}
使用performSelector:withObject:afterDelay:和cancelPreviousPerformRequestsWithTarget
使用這種方式, 會在連續(xù)重復(fù)點(diǎn)擊UIButton的時(shí)候, 自動(dòng)取消掉之前的操作, 延時(shí)1s后執(zhí)行實(shí)際的操作.
這樣, 看起來會比第一種和第三種稍微延遲執(zhí)行實(shí)際的操作.
[btn addTarget:self action:@selector(actionFixMultiClick_performSelector:) for
// xxx
- (void)actionFixMultiClick_performSelector:(UIButton *)sender {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(btnClickedOperations) object:nil];
[self performSelector:@selector(btnClickedOperations) withObject:nil afterDelay:1];
}
使用runtime來對sendAction:to:forEvent:方法進(jìn)行hook
UIControl的sendAction:to:forEvent:方法用于處理事件響應(yīng).
如果我們在該方法的實(shí)現(xiàn)中, 添加針對點(diǎn)擊事件的時(shí)間間隔相關(guān)的處理代碼, 則能夠做到在指定時(shí)間間隔中防止重復(fù)點(diǎn)擊.
首先, 為UIButton添加一個(gè)Category:
@interface UIButton (CS_FixMultiClick)
@property (nonatomic, assign) NSTimeInterval cs_acceptEventInterval; // 重復(fù)點(diǎn)擊的間隔
@property (nonatomic, assign) NSTimeInterval cs_acceptEventTime;
@end
Category不能給類添加屬性, 所以以上的cs_acceptEventInterval和cs_acceptEventTime只會有對應(yīng)的getter和setter方法, 不會添加真正的成員變量.
如果我們不在實(shí)現(xiàn)文件中添加其getter和setter方法, 則采用*** btn.cs_acceptEventInterval = 1; *** 這種方法嘗試訪問該屬性會出錯(cuò).
2016-06-29 14:04:52.538 DemoRuntime[17380:1387981] -[UIButton setCs_acceptEventInterval:]: unrecognized selector sent to instance 0x7fe8e154e470
在實(shí)現(xiàn)文件中通過runtime的關(guān)聯(lián)對象的方式, 為UIButton添加以上兩個(gè)屬性. 代碼如下:
#import "UIControl+CS_FixMultiClick.h"
#import <objc/runtime.h>
@implementation UIButton (CS_FixMultiClick)
// 因category不能添加屬性辖试,只能通過關(guān)聯(lián)對象的方式汇跨。
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
- (NSTimeInterval)cs_acceptEventInterval {
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setCs_acceptEventInterval:(NSTimeInterval)cs_acceptEventInterval {
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(cs_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
static const char *UIControl_acceptEventTime = "UIControl_acceptEventTime";
- (NSTimeInterval)cs_acceptEventTime {
return [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue];
}
- (void)setCs_acceptEventTime:(NSTimeInterval)cs_acceptEventTime {
objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cs_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
// 在load時(shí)執(zhí)行hook
+ (void)load {
Method before = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method after = class_getInstanceMethod(self, @selector(cs_sendAction:to:forEvent:));
method_exchangeImplementations(before, after);
}
- (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if ([NSDate date].timeIntervalSince1970 - self.cs_acceptEventTime < self.cs_acceptEventInterval) {
return;
}
if (self.cs_acceptEventInterval > 0) {
self.cs_acceptEventTime = [NSDate date].timeIntervalSince1970;
}
[self cs_sendAction:action to:target forEvent:event];
}
@end
load方法是在objc庫中的一個(gè)load_images函數(shù)中調(diào)用的. 先把二進(jìn)制映像文件中的頭信息取出, 再解析和讀出各個(gè)模塊中的類定義信息, 把實(shí)現(xiàn)了load方法的類和Category記錄下來, 最后統(tǒng)一執(zhí)行調(diào)用. 主類中的load方法的調(diào)用時(shí)機(jī)要早于Category中的load方法.
關(guān)于load和initialize方法, 可參看博客NSObject的load和initialize方法.
因此, 我們在Category中的load方法中, 執(zhí)行runtime的method swizzling, 即可將UIButton的事件響應(yīng)方法sendAction:to:forEvent:替換為我們自定義的方法cs_sendAction:to:forEvent:.
關(guān)于runtime的關(guān)聯(lián)對象和method swizzling, 這里就不多做介紹了, 可參考博客iOS --- 理解Runtime機(jī)制及其使用場景.
那么, 如何使用呢?
btn.cs_acceptEventInterval = 1;
這樣, 就給UIButton指定了1s的時(shí)間間隔用于防止重復(fù)點(diǎn)擊.
總結(jié)
三種方式中推薦使用runtime方式, 這樣可以為所有的UIButton同時(shí)添加.
有些場景下, 可以考慮使用第二種方式, 自動(dòng)延遲后以最終的一次點(diǎn)擊事件為準(zhǔn)執(zhí)行實(shí)際操作.
Demo請參考DemoRuntime.