iOS小技巧--用runtime 解决UIButton 重复点击问题
来源:互联网 发布:js 指定日期的时间戳 编辑:程序博客网 时间:2024/05/16 07:55
什么是这个问题
我们的按钮是点击一次响应一次, 即使频繁的点击也不会出问题, 可是某些场景下还偏偏就是会出问题.
通常是如何解决
我们通常会在按钮点击的时候设置这个按钮不可点击. 等待0.xS的延时后,在设置回来; 或者在操作结束的时候设置可以点击.
1
2
3
4
5
6
- (IBAction)clickBtn1:(UIbutton *)sender
{
sender.enabled = NO;
doSomething
sender.enabled = YES;
}
如果涉及到按钮不同状态不同样式的时候, 用enabled不见得够用.还得额外加个变量来记录状态.
1
2
3
4
5
6
7
- (IBAction)clickBtn1:(UIbutton *)sender
{
if
(doingSomeThing)
return
;
doingSomeThing = YES;
doSomething
doingSomeThing = NO;
}
笔者举的例子是直接在响应事件的周期内直接禁止点击的. 如果想做1秒内禁止重复点击的话,则得用performSelector:withObject:afterDelay:
漂亮的解决是怎样的
有了重复的代码段就是有了一个共性, 就可以抽象出来.
我们可以给按钮添加一个属性重复点击间隔, 通过设置这个属性来控制再次接受点击事件的时间间隔.
1
2
3
4
5
6
7
8
9
10
11
12
@interface UIControl (XY)
@property (nonatomic, assign) NSTimeInterval uxy_acceptEventInterval;
// 可以用这个给重复点击加间隔
@end
static 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
1
2
3
4
5
6
7
8
@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
在我们的点击事件里呢,对点击事件做下过滤
1
2
3
4
5
6
7
8
9
10
- (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];
}
实际使用起来就是这个样子
1
2
3
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tempBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside];
tempBtn.uxy_acceptEventInterval = 0.5;
文章至此就结束了.虽然不推荐大范围用runtime, 但是小范围内使用还是可以解决不少小问题的.
0 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点击范围
- Web Real-Time Communication(WebRTC)技术概述
- Linux中TCP连接过程状态简介
- 练习一html注册页面输入验证
- 微信公众平台开发教程第21篇-“可信网址”白名单
- iptables命令格式
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- CocosPod导入FFmpeg本地库的经历
- Binary Tree Preorder Traversal —— Leetcode
- google 的 gson json字符串转map 或 list
- 虚拟机下搭建IOS开发环境
- 泛类型的继承
- Redis学习5之redis-cli使用方法总结
- leetcode Plus One
- Java字符串找出4个字节长度的字符