一.设置button 属性
- (void)buttonClicked:(UIButton *)sender {
sender.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
sender.enabled = YES;
});
}
二.通过NSObject的+cancelPreviousPerformRequestsWithTarget:selector:object:方法和-performSelector:withObject:afterDelay:方法
这个方法如果设置时间是2秒,每次点击间隔小于2秒的话,它会一直取消前一个事件,直到点击间隔大于2秒才会执行事件,造成延时
- (void)buttonClicked:(UIButton *)sender {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonClickedAction:) object:sender];
[self performSelector:@selector(buttonClickedAction:) withObject:sender afterDelay:2.0];
}
三.用runtime 给button 添加 时间间隔属性
.h 文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIButton (YN_FixMultiClick)
@property (nonatomic, assign) NSTimeInterval yn_acceptEventInterval; // 重复点击的间隔
@end
NS_ASSUME_NONNULL_END
.m文件
#import "UIButton+YN_FixMultiClick.h"
#import <objc/runtime.h>
#define defaultInterval 0.0003 //默认时间间隔,不能太长,以免影响系统拍照等功能
@interface UIButton ()
/**
* bool YES 忽略点击事件 NO 允许点击事件
*/
@property (nonatomic, assign) BOOL isIgnoreEvent;
@end
@implementation UIButton (YN_FixMultiClick)
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const char *UIControl_enventIsIgnoreEvent = "UIControl_enventIsIgnoreEvent";
+ (void)load {
// Method Swizzling
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//根据selector查找Method
SEL selA = @selector(sendAction:to:forEvent:);
SEL selB = @selector(new_sendAction: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{//添加不成功->交换
//添加失败了 说明本类中有methodB的实现,此时只需要将methodA和methodB的IMP互换一下即可。
method_exchangeImplementations(methodA, methodB);
}
});
}
- (void)new_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
self.yn_acceptEventInterval = self.yn_acceptEventInterval == 0 ? defaultInterval : self.yn_acceptEventInterval;
if (self.isIgnoreEvent){//默认可以响应点击事件
return;
}else if (self.yn_acceptEventInterval > 0){//第一次点击,设定eventTimeInterval后,可以响应点击事件
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.yn_acceptEventInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self setIsIgnoreEvent:NO];
// });
[self performSelector:@selector(setIsIgnoreEvent:) withObject:@(NO) afterDelay:self.yn_acceptEventInterval];
}
self.isIgnoreEvent = YES;//设置不可以响应点击事件
[self new_sendAction:action to:target forEvent:event];
}
// runtime 动态绑定 属性
- (BOOL)isIgnoreEvent{
return [objc_getAssociatedObject(self, UIControl_enventIsIgnoreEvent) boolValue];
}
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent {
objc_setAssociatedObject(self, UIControl_enventIsIgnoreEvent, @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)setYn_acceptEventInterval:(NSTimeInterval)yn_acceptEventInterval{
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(yn_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSTimeInterval)yn_acceptEventInterval{
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
@end
使用
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [[UIButton alloc]init];
[self.view addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset = 100;
make.left.offset = 100;
make.width.height.offset = 100;
}];
btn.backgroundColor = UIColor.redColor;
btn.yn_acceptEventInterval = 3;
[btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
}
-(void)clickBtn{
NSLog(@"点击了Btn");
}
效果
image.png
连续点击的时候3秒一次响应
网友评论