UIButton小技巧----点击事件时间间隔
来源:互联网 发布:改号软件 编辑:程序博客网 时间:2024/05/09 16:38
UIButton小技巧—-点击事件时间间隔
起因
在开发过程中对于UIbutton的点击事件,如果进行频繁的点击,可能会造成事件的不必要的重复执行事件,甚至造成不必要的错误。
解决方案
通过添加Category,重写sendAction:to:forEvent:
方法。(通过runtime
交换系统的sendAction:to:forEvent:
和 自定义的customSendAction:to:forEvent:
方法,在定义方法中进行时间判断是否符合时间间隔需求,不符合直接return
否则在调用系统的sendAction:to:forEvent:
)。
代码
#import <UIKit/UIKit.h>@interface UIButton (TimeInterval)@property NSTimeInterval repeatEventInterval;@end
#import "UIButton+TimeInterval.h"#import <objc/runtime.h>const char *repeatEventIntervalKey = "repeatEventIntervalKey";const char *previousClickTimeKey = "previousClickTimeKey";@implementation UIButton (TimeInterval)+ (void)load { Method sendAction = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:)); Method customSendAction = class_getInstanceMethod([self class], @selector(customSendAction:to:forEvent:)); method_exchangeImplementations(sendAction, customSendAction);}- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { [super sendAction:action to:target forEvent:event];}- (void)setRepeatEventInterval:(NSTimeInterval)repeatEventInterval { objc_setAssociatedObject(self, repeatEventIntervalKey, @(repeatEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSTimeInterval)repeatEventInterval { return (NSTimeInterval)[objc_getAssociatedObject(self, repeatEventIntervalKey) doubleValue];}- (void)setPreviousClickTime:(NSTimeInterval)previousClickTime { objc_setAssociatedObject(self, previousClickTimeKey, @(previousClickTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSTimeInterval)previousClickTime { return [objc_getAssociatedObject(self, previousClickTimeKey) doubleValue];}- (void)customSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event { if ( NSDate.date.timeIntervalSince1970 - self.previousClickTime < self.repeatEventInterval ) { return; } if (self.repeatEventInterval > 0) { self.previousClickTime = NSDate.date.timeIntervalSince1970 ; } [self customSendAction:action to:target forEvent:event];}@end
应用
#import "UIButton+TimeInterval.h"button.repeatEventInterval = 2;
阅读全文
0 0
- UIButton小技巧----点击事件时间间隔
- UIButton小技巧----点击事件的范围
- Vickate_iOS_设置 UIButton 点击时间间隔,防止连续重复点击
- [小技巧] ping 的时间间隔
- 加入点击时间间隔的点击事件IntervalOnClick
- UIButton点击事件不起作用
- UIBUTTON点击事件
- UIButton 的点击事件
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- 小技巧之-runtime 解决 UIButton 重复点击问题
- iOS小技巧:禁止全屏点击事件
- UIButton 小技巧setTitleEdgeInsets
- UIButton 小技巧setTitleEdgeInsets
- UIButton 小技巧setTitleEdgeInsets
- 在0.2秒时间间隔内多次点击只响应一次点击事件
- JQuery点击事件添加间隔
- python2转化成python3脚本
- GSON你应该知道的事
- python编程 从入门到实践第七章 输入与while循环
- matlb加载图像库运行实验
- android jni
- UIButton小技巧----点击事件时间间隔
- [教你警告消除]Warning[Pe1072]: a declaration cannot have a label!
- Git push报错,提示没有权限.
- 数据库数据传给页面处理
- 第十四天 流程结构(上)
- 手机WiFi三点定位系统android
- std::bind()的使用
- getZTreeObj为null情况(getCheckedNodes报错)
- Linux系统控制