小技巧之-runtime 解决 UIButton 重复点击问题
来源:互联网 发布:手机如何开淘宝店步骤 编辑:程序博客网 时间:2024/04/29 11:44
什么是这个问题
我们的按钮是点击一次响应一次, 即使频繁的点击也不会出问题, 可是某些场景下还偏偏就是会出问题。
通常是如何解决
我们通常会在按钮点击的时候设置这个按钮不可点击. 等待0.xS的延时后,在设置回来; 或者在操作结束的时候设置可以点击。
- (IBAction)clickBtn1:(UIbutton *)sender{ sender.enabled = NO; doSomething sender.enabled = YES;}
如果涉及到按钮不同状态不同样式的时候, 用enabled不见得够用.还得额外加个变量来记录状态。
- (IBAction)clickBtn1:(UIbutton *)sender{ if (doingSomeThing) return; doingSomeThing = YES; doSomething doingSomeThing = NO;}
笔者举的例子是直接在响应事件的周期内直接禁止点击的. 如果想做1秒内禁止重复点击的话,则得用performSelector:withObject:afterDelay:
漂亮的解决是怎样的
有了重复的代码段就是有了一个共性, 就可以抽象出来。
我们可以给按钮添加一个属性重复点击间隔, 通过设置这个属性来控制再次接受点击事件的时间间隔。
@interface UIControl (XY)@property (nonatomic, assign) NSTimeInterval uxy_acceptEventInterval; // 可以用这个给重复点击加间隔@endstatic const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";- (NSTimeInterval)uxy_acceptEventInterval{ return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];}- (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval{ objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}
在app启动的时候,我们hook 所有的按钮的 event
@implementation UIControl (XY)+ (void)load{ Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:)); Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:)); method_exchangeImplementations(a, b);}@end
在我们的点击事件里呢,对点击事件做下过滤
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{ if (self.uxy_ignoreEvent) return; if (self.uxy_acceptEventInterval > 0) { self.uxy_ignoreEvent = YES; [self performSelector:@selector(setUxy_ignoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval]; } [self __uxy_sendAction:action to:target forEvent:event];}
实际使用起来就是这个样子
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];[tempBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside];tempBtn.uxy_acceptEventInterval = 0.5;
文章至此就结束了.虽然不推荐大范围用runtime, 但是小范围内使用还是可以解决不少小问题的。
0 0
- 小技巧之-runtime 解决 UIButton 重复点击问题
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- iOS小技巧–用runtime 解决UIButton 重复点击问题
- 用runtime 解决UIButton 重复点击问题
- 用Runtime解决UIButton重复点击问题
- 用runtime 解决UIButton 重复点击问题
- iOS之使用Runtime解决UIButton多次点击(重复点击)
- Runtime优雅的解决UIButton多次点击(重复点击)
- iOS 基于runtime 解决UIButton重复点击 发送多次请求问题
- OC runtime 机制消除UIButton重复点击问题
- runtime - UIButton点击事件重复处理
- UIButton小技巧----点击事件时间间隔
- UIButton小技巧----点击事件的范围
- runtime解决button重复点击的bug
- UIbutton 防止重复点击
- 防止UIButton重复点击
- 学习Wireshark(九):应用Wireshark显示过滤器分析特定数据流(上)
- sqlite的联表查询-转
- 打包时遇见的错误--App配置为iOS路由App
- android studio preference 在哪
- c/c++ 时间函数总结 linux-转
- 小技巧之-runtime 解决 UIButton 重复点击问题
- ELF文件格式与相关命令
- Linux经典书籍推荐-转
- SVM从原理到SMO实现(分类)
- snmp 编译
- UI02_UIImageView
- 3.6 产生式系统
- 在javascript中,哪些值能作为if的条件
- 复杂多目录的Makefile模板及示例-转