#import <UIKit/UIKit.h>
@interface UIButton (TimeInterval)
/** 点击时间间隔: 单位(秒) */
@property (nonatomic, assign) NSTimeInterval timeInterval;
@end
#import "UIButton+TimeInterval.h"
#import <objc/runtime.h>
@interface UIButton ()
/** 是否忽略响应事件 */
@property(nonatomic, assign) BOOL isIgnoreEvent;
@end
@implementation UIButton (TimeInterval)
/**
应用启动时,hook住所有按钮的event
*/
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selA = @selector(sendAction:to:forEvent:);
SEL selB = @selector(mySendAction:to:forEvent:);
Method methodA = class_getInstanceMethod(self,selA);
Method methodB = class_getInstanceMethod(self, selB);
BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
if (isAdd) {
class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
}
else {
method_exchangeImplementations(methodA, methodB);
}
});
}
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
if (self.isIgnoreEvent) {
return;
}
if (self.timeInterval > 0) {
self.isIgnoreEvent = YES;
[self performSelector:@selector(setIsIgnoreEvent:) withObject:@(NO) afterDelay:self.timeInterval];
}
}
[self mySendAction:action to:target forEvent:event];
}
// MARK: - 运行时设置分类属性
- (NSTimeInterval)timeInterval {
return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setTimeInterval:(NSTimeInterval)timeInterval {
objc_setAssociatedObject(self, @selector(timeInterval), @(timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent {
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent {
objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
测试
#import "ViewController.h"
#import "UIButton+TimeInterval.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *btn = [[UIButton alloc] init];
[self.view addSubview:btn];
[btn setTitle:@"点我" forState:UIControlStateNormal];
[btn sizeToFit];
btn.center = self.view.center;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
// 关键属性,设置每隔多少秒按钮可点
btn.timeInterval = 2;
}
- (void)btnClick:(UIButton*)sender{
NSLog(@"xxxxxx");
}
@end