防止點(diǎn)擊事件重復(fù)點(diǎn)擊的方法

一:

#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];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容