UIButton防止多次点击
不多bb,直接上干货
#import NS_ASSUME_NONNULL_BEGIN@interface UIControl (EventInterval)/// 按钮响应间隔
@property (nonatomic, assign) NSTimeInterval eventInterval;
@endNS_ASSUME_NONNULL_END
#import "UIControl+EventInterval.h"
#import static char *const eventIntervalKey = "eventIntervalKey";
static char *const eventUnavailableKey = "eventUnavailableKey";
static NSTimeInterval const defaultEventInterval = 0;
@interface UIControl ()@property (nonatomic, assign) BOOL eventUnavailable;
@end@implementation UIControl (EventInterval)+ (void)load {
Method method = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method gf_method = class_getInstanceMethod(self, @selector(gf_sendAction:to:forEvent:));
method_exchangeImplementations(method, gf_method);
}#pragma mark - ActionFun
- (void)gf_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if ([self isKindOfClass:[UIButton class]]) {
if (self.eventUnavailable == NO) {
self.eventUnavailable = YES;
[self gf_sendAction:action to:target forEvent:event];
[self performSelector:@selector(setEventUnavailable:) withObject:@(NO) afterDelay:self.eventInterval > 0 ? self.eventInterval : defaultEventInterval];
}
}
else{
[self gf_sendAction:action to:target forEvent:event];
}
}#pragma mark - setter & getter
- (NSTimeInterval)eventInterval {
return [objc_getAssociatedObject(self, eventIntervalKey) doubleValue];
}- (void)setEventInterval:(NSTimeInterval)eventInterval {
objc_setAssociatedObject(self, eventIntervalKey, @(eventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}- (BOOL)eventUnavailable {
return [objc_getAssociatedObject(self, eventUnavailableKey) boolValue];
}- (void)setEventUnavailable:(BOOL)eventUnavailable {
objc_setAssociatedObject(self, eventUnavailableKey, @(eventUnavailable), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}@end
推荐阅读
- android防止连续点击的简单实现(kotlin)
- 2018-12-03-新手教程重构思路
- 防止sql注入
- UIbutton防止重复点击
- 微信内如何防止推广链接被封,微信内随机跳转落地页的实现原理
- 服务器|用旧手机搭建服务器并实现内网穿透不需要root(本人亲测很多次最简单的一个)
- UIButton:|UIButton: 巧妙封装截获响应
- 读《财富自由之路》笔记2021-05-18把一份时间出售多次
- iOS|iOS UIButton设置button上的文字和图片
- 个案收获