我的git地址:https://github.com/smileshang/simpleTools.git
包含了多個自己總結出來的工具類,喜歡的給個star,謝謝。
一、避免屏幕內多個button被同時點擊
1、在AppDelegate中添加 [[UIButton appearance] setExclusiveTouch:YES];
2、或者每新建button都設置button.exclusiveTouch = YES;
二、避免一個button被多次點擊(共總結了3種)
第一種:使用runtime,一勞永逸 我這設的是0.5秒內不會被重復點擊
1、導入objc/runtime.h(我放在了pch文件里)
2、創建uicontrol或uibutton的分類,(what?!! 你告訴我不會?!)
創建分類文件:1、打開Xcode,新建文件,選擇OC文件
1.png
2、在第二個界面,將file Type 選為 Category類,在class里選繼承的類別,這里咱們選的是UIcontrol
2.png
注:control類是對所有button的點擊事件反應,若用Unbutton類,則只會對Unbutton創建的button反應。
3、好了 分類創建完畢 對分類進行操作
1、.h文件 這里只貼圖,最后放代碼們
3.png
2、.m文件
4.png
5.png
最后是代碼們
#import
#define defaultInterval.5//默認時間間隔
@interfaceUIControl (UIControl_buttonCon)
@property(nonatomic,assign)NSTimeIntervaltimeInterval;//用這個給重復點擊加間隔
@property(nonatomic,assign)BOOLisIgnoreEvent;//YES不允許點擊NO允許點擊
@end
#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動態綁定屬性
- (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的實現添加到系統方法中也就是說將methodA方法指針添加成方法methodB的返回值表示是否添加成功
BOOLisAdd =class_addMethod(self, selA,method_getImplementation(methodB),method_getTypeEncoding(methodB));
//添加成功了說明本類中不存在methodB所以此時必須將方法b的實現指針換成方法A的,否則b方法將沒有實現。
if(isAdd) {
class_replaceMethod(self, selB,method_getImplementation(methodA),method_getTypeEncoding(methodA));
}else{
//添加失敗了說明本類中有methodB的實現,此時只需要將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互換了,實際上執行sendAction;所以不會死循環
self.isIgnoreEvent=YES;
[selfmySendAction:actionto:targetforEvent:event];
}
@end
第二種:在每次點擊時先取消之前的操作
將這段代碼放在你按鈕點擊的方法中,例如:
- (void)buttonClicked:(id)sender
{
//點擊按鈕后先取消之前的操作,再進行需要進行的操作
[[selfclass]cancelPreviousPerformRequestsWithTarget:selfselector:@selector(buttonClicked:)object:sender];
[selfperformSelector:@selector(buttonClicked: )withObject:senderafterDelay:0.2f];
}
第三種:點擊后設為不可被點擊的狀態,幾秒后恢復
-(void)buttonClicked:(id)sender{
self.button.enabled =NO;
[selfperformSelector:@selector(changeButtonStatus)withObject:nilafterDelay:1.0f];//防止重復點擊
}
-(void)changeButtonStatus{
self.button.enabled =YES;
}