ios避免button多次连续点击跳转到多个页面
2017-06-26 09:47
393 查看
说明:这个代码我是网上找来的,忘记了出处了,抱歉
1、创建一个名称为Click的button分类
UIbutton+Click.h
UIbutton+Click.m
2、然后把这个类别加入工程,此时button的点击间隔为1秒。
也可以这样:button.eventTimeInterval = 5;
1、创建一个名称为Click的button分类
UIbutton+Click.h
#import <UIKit/UIKit.h> @interface UIButton (WXD) /** * 为按钮添加点击间隔 eventTimeInterval秒 */ @property (nonatomic, assign) NSTimeInterval eventTimeInterval; @end
UIbutton+Click.m
#import "UIButton+WXD.h" #import <objc/runtime.h> #define defaultInterval 5 //默认时间间隔 @interface UIButton () /** * bool YES 忽略点击事件 NO 允许点击事件 */ @property (nonatomic, assign) BOOL isIgnoreEvent; @end @implementation UIButton (WXD) static const char *UIControl_eventTimeInterval = "UIControl_eventTimeInterval"; static const char *UIControl_enventIsIgnoreEvent = "UIControl_enventIsIgnoreEvent"; // runtime 动态绑定 属性 - (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent { objc_setAssociatedObject(self, UIControl_enventIsIgnoreEvent, @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (BOOL)isIgnoreEvent { return [objc_getAssociatedObject(self, UIControl_enventIsIgnoreEvent) boolValue]; } - (NSTimeInterval)eventTimeInterval { return [objc_getAssociatedObject(self, UIControl_eventTimeInterval) doubleValue]; } - (void)setEventTimeInterval:(NSTimeInterval)eventTimeInterval { objc_setAssociatedObject(self, UIControl_eventTimeInterval, @(eventTimeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } + (void)load { // Method Swizzling static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SEL selA = @selector(sendAction:to:forEvent:); SEL selB = @selector(_wxd_sendAction:to:forEvent:); Method methodA = class_getInstanceMethod(self,selA); Method methodB = class_getInstanceMethod(self, selB); BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB)); if (isAdd) { class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA)); }else{ //添加失败了 说明本类中有methodB的实现,此时只需要将methodA和methodB的IMP互换一下即可。 method_exchangeImplementations(methodA, methodB); } }); } - (void)_wxd_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { self.eventTimeInterval = self.eventTimeInterval == 0 ? defaultInterval : self.eventTimeInterval; if (self.isIgnoreEvent){ return; }else if (self.eventTimeInterval > 0){ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.eventTimeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self setIsIgnoreEvent:NO]; }); } self.isIgnoreEvent = YES; // 这里看上去会陷入递归调用死循环,但在运行期此方法是和sendAction:to:forEvent:互换的,相当于执行sendAction:to:forEvent:方法,所以并不会陷入死循环。 [self _wxd_sendAction:action to:target forEvent:event]; }
2、然后把这个类别加入工程,此时button的点击间隔为1秒。
也可以这样:button.eventTimeInterval = 5;
相关文章推荐
- iOS开发--利用 runtime,解决多次点击相同 button,导致重复跳转的问题
- iOS- 利用runtime,解决多次点击相同button,导致重复跳转的问题
- 完全解决快速连续多次点击button导致跳转多个界面(注:方法从他人博客中看到,稍作修改)
- iOS极光推送 点击推送消息跳转页面
- js点击button按钮跳转到页面代码
- Button避免被连续点击
- 防止快速连续点击button多次执行相同操作
- [IOS 开发] 防止button被点击多次
- ios 防止按钮快速点击造成多次响应的避免方法。
- iOS APNS 极光推送 点击通知栏跳转应用相应页面
- iOS 开发之 点击tabbarItem添加是否跳转登录页面判断
- js点击button按钮跳转到页面代码
- js点击button按钮跳转到页面代码
- iOS 实现点击tabbar跳转登录页面
- iOS极光推送 点击推送消息跳转页面
- 关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理
- iOS 开发之 点击tabbarItem添加是否跳转登录页面判断
- ios 防止按钮快速点击造成多次响应的避免方法。
- ios 防止按钮快速点击造成多次响应的避免方法
- js点击button跳转到另一个页面