一:
#import
@interfaceUIButton (CS_FixMultiClick)
@property(nonatomic,assign)NSTimeIntervalcs_acceptEventInterval;// 重復(fù)點(diǎn)擊的間隔
@property(nonatomic,assign)NSTimeIntervalcs_acceptEventTime;
@end
#import "UIButton+CS_FixMultiClick.h"
#import
@implementationUIButton (CS_FixMultiClick)
// 因category不能添加屬性,只能通過關(guān)聯(lián)對(duì)象的方式。
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時(shí)執(zhí)行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點(diǎn)擊事件中寫這個(gè)
[[selfclass]cancelPreviousPerformRequestsWithTarget:selfselector:@selector(buttonClicked:)object:sender];
? [selfperformSelector:@selector(buttonClicked: )withObject:senderafterDelay:0.2f];