方案一:
https://github.com/kingsic/SGEasyButton.git
这个网友提供了一种方案,但是用起来并不是很合意。
方案二:这个偶尔还是很实用的,利用runtime运行时机制,让按钮点击之后的短时间内不再响应此事件
#import <UIKit/UIKit.h>
#define defaultInterval 0.25//默认时间间隔
@interface UIButton (Countdown)
@property(nonatomic,assign)NSTimeInterval timeInterval;//用这个给重复点击加间隔
@property(nonatomic,assign)BOOL isIgnoreEvent;//YES不允许点击NO允许点击
@end
#import "UIButton+Countdown.h"
#import <objc/runtime.h>
@implementation UIButton (Countdown)
-(BOOL)isTouchInside
{
// self.enabled = NO;
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// self.enabled = YES;
// });
// funclog
return YES;
}
- (NSTimeInterval)timeInterval
{
return[objc_getAssociatedObject(self,_cmd)doubleValue];
}
- (void)setTimeInterval:(NSTimeInterval)timeInterval
{
objc_setAssociatedObject(self,@selector(timeInterval),@(timeInterval),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
//runtime动态绑定属性
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{
//注意BOOL类型需要用OBJC_ASSOCIATION_RETAIN_NONATOMIC不要用错,否则set方法会赋值出错
objc_setAssociatedObject(self,@selector(isIgnoreEvent),@(isIgnoreEvent),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent{
//_cmd == @select(isIgnore);和set方法里一致
return[objc_getAssociatedObject(self,_cmd)boolValue];
}
- (void)resetState{
[self setIsIgnoreEvent:NO];
}
+ (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);
//将methodB的实现添加到系统方法中也就是说将methodA方法指针添加成方法methodB的返回值表示是否添加成功
BOOL isAdd =class_addMethod(self, selA,method_getImplementation(methodB),method_getTypeEncoding(methodB));
//添加成功了说明本类中不存在methodB所以此时必须将方法b的实现指针换成方法A的,否则b方法将没有实现。
if(isAdd) {
class_replaceMethod(self, selB,method_getImplementation(methodA),method_getTypeEncoding(methodA));
}else{
//添加失败了说明本类中有methodB的实现,此时只需要将methodA和methodB的IMP互换一下即可。
method_exchangeImplementations(methodA, methodB);
}
});
}
//当我们按钮点击事件sendAction时将会执行mySendAction
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event
{
if([self isKindOfClass:[UIButton class]]) {
self.timeInterval=self.timeInterval==0?defaultInterval:self.timeInterval;
if(self.isIgnoreEvent){
return;
}else if(self.timeInterval>0){
[self performSelector:@selector(resetState)withObject:nil afterDelay:self.timeInterval];
}
}
//此处methodA和methodB方法IMP互换了,实际上执行sendAction;所以不会死循环
self.isIgnoreEvent=YES;
[self mySendAction:action to:target forEvent:event];
}
个人还是很推荐方案2的
两个方案都写在一个Demo里面了
http://git.oschina.net/lanyingwei/codes/ehrvmwuc359bqgszda2jk77