1.方法一:
通過設置按鈕的enabled屬性.
- (IBAction)btn:(id)sender {
//點擊事件內部關閉 1s后再次響應
self.btn.enabled = NO;
[self performSelector:@selector(avoidRepeatClick) withObject:nil afterDelay:1];
self.count++;
NSLog(@"count= %ld",self.count);
}
- (void)avoidRepeatClick {
self.btn.enabled = YES;
}
2.方法二:
給UIButton添加一個分類, 用runtime方法.
.h文件
#import <UIKit/UIKit.h>
@interface UIButton (HETouch)
/**
時間間隔, 控制重復點擊的間隔時間
*/
@property (nonatomic, assign) NSTimeInterval he_timeInterval;
@end
.m文件
#import "UIButton+HETouch.h"
#import <objc/runtime.h>
#define DEFAULT_INTERVAL .5 //默認時間間隔
@interface UIButton ()
/**
是否忽視重復點擊, YES - 忽視重復點擊 NO - 不忽視重復點擊
*/
@property (nonatomic, assign) BOOL isIgnoreEvent;
@end
@implementation UIButton (HETouch)
#pragma mark -
#pragma mark - load, 交換響應的點擊方法
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 1. 獲取系統的方法
SEL selSystem = @selector(sendAction:to:forEvent:);
Method methodSystem = class_getInstanceMethod(self, selSystem);
// 2. 獲取自定義的方法
SEL selCustom = @selector(heSendAction:to:forEvent:);
Method methodCustom = class_getInstanceMethod(self, selCustom);
// 3. 添加一個覆蓋父類的實現,但不會取代現有的實現類, 返回是否成功
BOOL isAdd = class_addMethod(self, selSystem, method_getImplementation(methodCustom), method_getTypeEncoding(methodCustom));
// 4. 判斷是要完全替換原有的方法, 還是只是交換兩個方法的實現
if (isAdd) {
// 替換原來的方法
class_replaceMethod(self, selCustom, method_getImplementation(methodSystem), method_getTypeEncoding(methodSystem));
} else {
// 交換兩個方法的實現
method_exchangeImplementations(methodSystem, methodCustom);
}
});
}
#pragma mark -
#pragma mark - 自定義方法, 用于替換系統的sendAction:to:forEvent: 方法
- (void)heSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
// 1. 判斷對象類型
if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
// 2. 賦值間隔時間
self.he_timeInterval = self.he_timeInterval == 0 ? DEFAULT_INTERVAL : self.he_timeInterval;
// 3. 判斷是否需要忽視重復點擊
if (self.isIgnoreEvent) return;
// 4. 判斷是否間隔時間大于0, 大于0的時候在經過間隔時間之后設置isIgnoreEvent的值為NO(不忽視點擊, 就是允許重復點擊)
if (self.he_timeInterval > 0) {
[self performSelector:@selector(changeIgnoreState) withObject:self afterDelay:self.he_timeInterval];
}
}
// 5. 設置忽視重復點擊(當重復點擊的時候就會執行 if (self.isIgnoreEvent) return; )
self.isIgnoreEvent = YES;
// 6. 由于交換了方法的實現, 所以以下的方法其實是 sendAction:to:forEvent:
[self heSendAction:action to:target forEvent:event];
}
#pragma mark -
#pragma mark 改變忽視重復點擊狀態
- (void)changeIgnoreState {
[self setIsIgnoreEvent:NO];
}
#pragma mark -
#pragma mark - he_timeInterval的setter, getter
- (void)setHe_timeInterval:(NSTimeInterval)he_timeInterval {
objc_setAssociatedObject(self, @selector(he_timeInterval), @(he_timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSTimeInterval)he_timeInterval {
/*
_cmd 代表本方法的SEL, 就是 @selector(he_timeInterval)
objc_getAssociatedObject(self, _cmd) 返回的是id類型, 需要類型轉換
*/
return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
#pragma mark -
#pragma mark - he_timeInterval的setter, getter
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent {
objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent {
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
@end
使用的時候設置一下響應時間:
self.btn.he_timeInterval = 2.0;