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;
推荐阅读
- android防止连续点击的简单实现(kotlin)
- 第5关(消灭该死的重复(上))
- 2018-12-03-新手教程重构思路
- iOS|iOS 本地推送开发记录二
- 防止sql注入
- python日志重复输出
- 这一场春雪
- [Py023]List元素重复
- JavaScript|JavaScript - 子集1(回溯法)
- 微信内如何防止推广链接被封,微信内随机跳转落地页的实现原理