參考文章
一音瓷、使用UIButton的enabled屬性, 在點擊后, 禁止UIButton的交互, 直到完成指定任務之后再將其enable即可.
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *testButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_testButton addTarget:self action:@selector(actionFixMultiClick_enabled:) forControlEvents:UIControlEventTouchUpInside];
}
- (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");
_testButton.enabled = YES;
});
}
二对嚼、使用performSelector:withObject:afterDelay:和cancelPreviousPerformRequestsWithTarget
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *testButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_testButton addTarget:self action:@selector(actionFixMultiClick_performSelector:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)actionFixMultiClick_performSelector:(UIButton *)sender {
NSLog(@"hhhhhhh");
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(btnClickedOperations) object:nil];
[self performSelector:@selector(btnClickedOperations) withObject:nil afterDelay:1];
}
- (void)btnClickedOperations {
self.view.backgroundColor = [UIColor colorWithRed:((arc4random() % 255) / 255.0)
green:((arc4random() % 255) / 255.0)
blue:((arc4random() % 255) / 255.0)
alpha:1.0f];
NSLog(@"啊哈哈f");
}
三、使用runtime來對sendAction:to:forEvent:方法進行hook
首先要了解分類的特性和Rutime機制
1绳慎、創(chuàng)建一個UIButton的分類
Button.h文件
#import <UIKit/UIKit.h>
@interface UIButton (YFFixMultiClick)
@property (nonatomic, assign) NSTimeInterval cs_acceptEventInterval; //重復點擊的間隔
@property (nonatomic, assign) NSTimeInterval cs_acceptEventTime;
@end
Button.m文件
#import "UIButton+YFFixMultiClick.h"
#import <objc/runtime.h>
@implementation UIButton (YFFixMultiClick)
// 因category不能添加屬性纵竖,只能通過關聯(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時執(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];
}
2、實現(xiàn)
#import "ViewController.h"
#import "UIButton+YFFixMultiClick.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *testButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[_testButton addTarget:self action:@selector(actionFixMultiClick_rutime) forControlEvents:UIControlEventTouchUpInside];
_testButton.cs_acceptEventInterval = 2;
}
-(void)actionFixMultiClick_rutime{
NSLog(@"哈哈");
}