UIbutton防止重复点击

【UIbutton防止重复点击】#import#define NAVIITEM_INTERVAL 0.5
@interfaceUIControl(Extension)//两次响应之间的时间间隔@property(nonatomic,assign)NSTimeIntervaluxy_acceptEventInterval; //是否忽略事件true 忽略 flase不忽略@property(nonatomic,strong)NSNumber*uxy_ignoreEvent; @end#import"UIControl+Extension.h"
@implementationUIControl(Extension)
static constchar*UIControl_acceptEventInterval="UIControl_acceptEventInterval";
static constchar*uxy_ignoreEventKey ="uxy_ignoreEventKey";
- (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);
}
-(NSNumber*)uxy_ignoreEvent{
returnobjc_getAssociatedObject(self, uxy_ignoreEventKey);
}
-(void)setUxy_ignoreEvent:(NSNumber*)uxy_ignoreEvent{
objc_setAssociatedObject(self, uxy_ignoreEventKey, uxy_ignoreEvent, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (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);
}
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event{
if(self.uxy_ignoreEvent.boolValue)return;
if(self.uxy_acceptEventInterval >0){
self.uxy_ignoreEvent = @(YES);
[selfperformSelector:@selector(setUxy_ignoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];
}
[self__uxy_sendAction:action to:target forEvent:event];
}
@end
在创建button的地方设置2次点击的间隔时间就可以了。如:
UIButton* btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn.uxy_acceptEventInterval = NAVIITEM_INTERVAL;

    推荐阅读