java 避免按键重复点击,button防止被重复点击的相关方法(详细版)

我的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文件

472a43b29b47

1.png

2、在第二个界面,将file Type 选为 Category类,在class里选继承的类别,这里咱们选的是UIcontrol

472a43b29b47

2.png

注:control类是对所有button的点击事件反应,若用Unbutton类,则只会对Unbutton创建的button反应。

3、好了 分类创建完毕 对分类进行操作

1、.h文件 这里只贴图,最后放代码们

472a43b29b47

3.png

2、.m文件

472a43b29b47

4.png

472a43b29b47

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值