iOS小技巧--用runtime 解决UIButton 重复点击问题
来源:互联网 发布:炒沥青怎么看金十数据 编辑:程序博客网 时间:2024/05/16 09:52
iOS小技巧–用runtime 解决UIButton 重复点击问题
什么是这个问题
我们的按钮是点击一次响应一次, 即使频繁的点击也不会出问题, 可是某些场景下还偏偏就是会出问题.
通常是如何解决
我们通常会在按钮点击的时候设置这个按钮不可点击. 等待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 (NSDate.date.timeIntervalSince1970 - self.uxy_acceptedEventTime < self.uxy_acceptEventInterval) return; if (self.uxy_acceptEventInterval > 0) { self.uxy_acceptedEventTime = NSDate.date.timeIntervalSince1970; } [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, 但是小范围内使用还是可以解决不少小问题的.
4 0
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- iOS小技巧–用runtime 解决UIButton 重复点击问题
- 小技巧之-runtime 解决 UIButton 重复点击问题
- 用runtime 解决UIButton 重复点击问题
- 用Runtime解决UIButton重复点击问题
- 用runtime 解决UIButton 重复点击问题
- iOS之使用Runtime解决UIButton多次点击(重复点击)
- iOS 基于runtime 解决UIButton重复点击 发送多次请求问题
- Runtime优雅的解决UIButton多次点击(重复点击)
- OC runtime 机制消除UIButton重复点击问题
- runtime - UIButton点击事件重复处理
- iOS开发- 利用runtime拦截UIButton的点击事件,防止重复点击
- iOS利用runtime,解决多次点击相同button,导致重复跳转的问题
- iOS- 利用runtime,解决多次点击相同button,导致重复跳转的问题
- iOS开发--利用 runtime,解决多次点击相同 button,导致重复跳转的问题
- iOS runtime扩大UIButton点击范围
- iframe请求失效重新登录后直接跳转到请求内容页面的解决方案
- 高阶MapReduce_2_链接多个MapReduce作业实例
- kiki’s game
- ex07
- 快速排序(含完整源码)
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- Codeforces 460C Present
- HDOJ 1114 Piggy-Bank(完全背包)
- css3的display定义不同类型的盒
- 手写wsdl文件
- UI初学 按钮
- 将图片转成字节流
- 模式匹配KMP算法
- java中static的使用