防止button被重復(fù)點(diǎn)擊

- (void)buttonClicked:(id)sender{

//點(diǎn)擊按鈕后先取消之前的操作,再進(jìn)行需要進(jìn)行的操作

[[selfclass]cancelPreviousPerformRequestsWithTarget:selfselector:@selector(buttonClicked:)object:sender];

[selfperformSelector:@selector(buttonClicked: )withObject:senderafterDelay:0.2f];

}


第二種:點(diǎn)擊后設(shè)為不可被點(diǎn)擊的狀態(tài),幾秒后恢復(fù):

-(void)buttonClicked:(id)sender{

self.button.enabled =NO;

[selfperformSelector:@selector(changeButtonStatus)withObject:nilafterDelay:1.0f];//防止重復(fù)點(diǎn)擊

}

-(void)changeButtonStatus{

self.button.enabled =YES;

}


第三種:使用runtime,一勞永逸我這設(shè)的是0.5秒內(nèi)不會(huì)被重復(fù)點(diǎn)擊

1.導(dǎo)入objc / runtime.h(可以放在PCH文件里)

2.創(chuàng)建uicontrol或UIButton的的分類!

創(chuàng)建分類文件:

2.1 打開(kāi)Xcode中,新建文件,選擇OC文件

2.2 在第二個(gè)界面,F(xiàn)ile名為UIControl+UIControl_buttonCon,將文件類型File Type選為Category類,在類里選繼承的類別,這里咱們選的Class是UIButton

注:若用Unbutton分類,則會(huì)對(duì)對(duì)Unbutton創(chuàng)建的按鈕反應(yīng)。

2.3 分類創(chuàng)建完畢對(duì)分類進(jìn)行操作

.h文件

#import

#define defaultInterval.5//默認(rèn)時(shí)間間隔

@interfaceUIControl (UIControl_buttonCon)

@property(nonatomic,assign)NSTimeIntervaltimeInterval;//用這個(gè)給重復(fù)點(diǎn)擊加間隔

@property(nonatomic,assign)BOOLisIgnoreEvent;//YES不允許點(diǎn)擊NO允許點(diǎn)擊

@end

.m文件

#import"UIControl+UIControl_buttonCon.h"

@implementationUIControl (UIControl_buttonCon)

- (NSTimeInterval)timeInterval{

return[objc_getAssociatedObject(self,_cmd)doubleValue];

}

- (void)setTimeInterval:(NSTimeInterval)timeInterval{

objc_setAssociatedObject(self,@selector(timeInterval),@(timeInterval),OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

//runtime動(dòng)態(tài)綁定屬性

- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{

objc_setAssociatedObject(self,@selector(isIgnoreEvent),@(isIgnoreEvent),OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (BOOL)isIgnoreEvent{

return[objc_getAssociatedObject(self,_cmd)boolValue];

}

- (void)resetState{

[selfsetIsIgnoreEvent:NO];

}

+ (void)load{

staticdispatch_once_tonceToken;

dispatch_once(&onceToken, ^{

SELselA =@selector(sendAction:to:forEvent:);

SELselB =@selector(mySendAction:to:forEvent:);

MethodmethodA =class_getInstanceMethod(self, selA);

MethodmethodB =class_getInstanceMethod(self, selB);

//將methodB的實(shí)現(xiàn)添加到系統(tǒng)方法中也就是說(shuō)將methodA方法指針添加成方法methodB的返回值表示是否添加成功

BOOLisAdd =class_addMethod(self, selA,method_getImplementation(methodB),method_getTypeEncoding(methodB));

//添加成功了說(shuō)明本類中不存在methodB所以此時(shí)必須將方法b的實(shí)現(xiàn)指針換成方法A的,否則b方法將沒(méi)有實(shí)現(xiàn)。

if(isAdd) {

class_replaceMethod(self, selB,method_getImplementation(methodA),method_getTypeEncoding(methodA));

}else{

//添加失敗了說(shuō)明本類中有methodB的實(shí)現(xiàn),此時(shí)只需要將methodA和methodB的IMP互換一下即可。

method_exchangeImplementations(methodA, methodB);

}

});

}

- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event{

if([NSStringFromClass(self.class)isEqualToString:@"UIButton"]) {

self.timeInterval=self.timeInterval==0?defaultInterval:self.timeInterval;

if(self.isIgnoreEvent){

return;

}elseif(self.timeInterval>0){

[selfperformSelector:@selector(resetState)withObject:nilafterDelay:self.timeInterval];

}

}

//此處methodA和methodB方法IMP互換了,實(shí)際上執(zhí)行sendAction;所以不會(huì)死循環(huán)

self.isIgnoreEvent=YES;

[selfmySendAction:actionto:targetforEvent:event];

}

@end

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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