一:
#import
@interfaceUIButton (CS_FixMultiClick)
@property(nonatomic,assign)NSTimeIntervalcs_acceptEventInterval;// 重复点击的间隔
@property(nonatomic,assign)NSTimeIntervalcs_acceptEventTime;
@end
#import "UIButton+CS_FixMultiClick.h"
#import
@implementationUIButton (CS_FixMultiClick)
// 因category不能添加属性,只能通过关联对象的方式。
staticconstchar*UIControl_acceptEventInterval ="UIControl_acceptEventInterval";
- (NSTimeInterval)cs_acceptEventInterval {
// return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
return 1;
}
- (void)setCs_acceptEventInterval:(NSTimeInterval)cs_acceptEventInterval {
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(cs_acceptEventInterval), OBJC_ASSOCIATION_ASSIGN);
}
staticconstchar*UIControl_acceptEventTime ="UIControl_acceptEventTime";
- (NSTimeInterval)cs_acceptEventTime {
return [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue];
}
- (void)setCs_acceptEventTime:(NSTimeInterval)cs_acceptEventTime {
objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cs_acceptEventTime), OBJC_ASSOCIATION_ASSIGN);
}
// 在load时执行hook
+ (void)load {
Methodbefore =class_getInstanceMethod(self,@selector(sendAction:to:forEvent:));
Methodafter =class_getInstanceMethod(self,@selector(cs_sendAction:to:forEvent:));
method_exchangeImplementations(before, after);
}
- (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event {
if ([NSDate date].timeIntervalSince1970 - self.cs_acceptEventTime < self.cs_acceptEventInterval) {
return;
}
if (self.cs_acceptEventInterval > 0) {
self.cs_acceptEventTime = [NSDate date].timeIntervalSince1970;
}
[selfcs_sendAction:actionto:targetforEvent:event];
}
@end
第二种:
- (IBAction)buttonActions:(UIButton*)sender{
sender.enable = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
sender.enabled=YES;
});
}
第三种 ,在button点击事件中写这个
[[selfclass]cancelPreviousPerformRequestsWithTarget:selfselector:@selector(buttonClicked:)object:sender];
[selfperformSelector:@selector(buttonClicked: )withObject:senderafterDelay:0.2f];
网友评论