ios解决button重复点击的问题
分类头文件:
#import <UIKit/UIKit.h>
@interface UIControl (CDControlCategory)
@property(nonatomic,assign)NSTimeInterval uxy_acceptEventInterval;// 可以用这个给重复点击加间隔
@property (nonatomic) BOOL ignoreEvent;
@end
----------分类实现:----------
#import "UIControl+HCDControlCategory.h"
#import <objc/runtime.h>
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const char *UIControl_ignoreEvent = "UIControl_ignoreEvent";
@implementation UIControl (HCDControlCategory)
//改变两个方法的实现。在类第一次使用的时候回调用这个方法
+(void)load{
Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:));
//改变两个方法的实现
method_exchangeImplementations(a, b);
}
//通过关联对象重写get和set方法
- (NSTimeInterval)uxy_acceptEventInterval
{
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval
{
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark 现在是否可点的get和set。通过关联对象。
-(void)setIgnoreEvent:(BOOL)ignoreEvent{
objc_setAssociatedObject(self, UIControl_ignoreEvent, @(ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(BOOL)ignoreEvent{
return [objc_getAssociatedObject(self, UIControl_ignoreEvent) boolValue];
}
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if (self.ignoreEvent) return;
if (self.uxy_acceptEventInterval > 0)
{
self.ignoreEvent = YES;
[self performSelector:@selector(setIgnoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];
}
//调用系统实现
[self __uxy_sendAction:action to:target forEvent:event];
}
@end
------------------ 使用 ---------------
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
tempBtn.frame = CGRectMake(100, 100, 30, 30);
tempBtn.backgroundColor = [UIColor greenColor];
[tempBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside];
tempBtn.uxy_acceptEventInterval = 2.5;
[self.view addSubview:tempBtn];
}
#pragma mark 这里其实就是调用我们自定义的那个方法。
-(void)clickWithInterval:(UIButton *)sender{
NSLog(@"你试试看。");
}