用RunTime来防止按钮被多次点击
来源:互联网 发布:手机可以恢复u盘数据吗 编辑:程序博客网 时间:2024/05/18 18:19
对于这个功能的实现是看了这个两个连接里的内容,主要是为UIButton增加一个延时的属性。
1、http://www.cocoachina.com/ios/20150911/13260.html
2、http://blog.sina.com.cn/s/blog_60342e330101tcz1.html
我这边总共做了两个,一个是创建UIButton的子类来实现,另一个是创建UIButton的cateorgy来实现。
第一个,创建UIButton的子类来实现
@interface MyButton :UIButton
@property (assign,nonatomic)NSTimeInterval timeInterval;
@end
@interface MyButton()
@property (assign,nonatomic)BOOL ignoreEvent;
@end
@implementation MyButton
+(void)load
{
Method a =class_getInstanceMethod(self,@selector(sendAction:to:forEvent:));
Method b =class_getInstanceMethod(self,@selector(timer_sendAction:to:forEvent:));
//是将a方法替换成b方法
method_exchangeImplementations(a, b);
}
-(void)timer_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
//如果是YES,规定的延时还没到
if (self.ignoreEvent) {
return;
}
if (self.timeInterval >0) {
//先设置这个参数为YES
self.ignoreEvent =YES;
//当指定延时时间到后,再将这个参数设为NO
[selfperformSelector:@selector(setIgnoreEvent:)withObject:@(NO)afterDelay:self.timeInterval];
}
[selftimer_sendAction:actionto:targetforEvent:event];
}
[self.buttonaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
//设置延时时间,单位秒
self.button.timeInterval =5;
@interface UIButton (ZM)
//设置延时时间
@property (assign,nonatomic)NSTimeInterval timeInterval;
@end
@implementation UIButton (ZM)
@dynamic timeInterval;
//是否可以再次点击 0代表未被点击 1代表已被点击
staticBOOL isClick;
+(void)load
{
Method a = class_getInstanceMethod(self,@selector(sendAction:to:forEvent:));
Method b = class_getInstanceMethod(self,@selector(timer_sendAction:to:forEvent:));
method_exchangeImplementations(a, b);
}
-(NSTimeInterval)timeInterval
{
return [objc_getAssociatedObject(self,@selector(timeInterval))doubleValue];
}
-(void)setTimeInterval:(NSTimeInterval)timeInterval
{
//第二个参数key,用@selector是因为SEL生成的时候就是一个唯一的常量
objc_setAssociatedObject(self,@selector(timeInterval),@(timeInterval),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)timer_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if (isClick) {
return;
}
if (self.timeInterval >0) {
isClick = YES;
//当指定延时时间到后,再将这个参数设为NO
[selfperformSelector:@selector(setIsClick:)withObject:@(NO)afterDelay:self.timeInterval];
}
[selftimer_sendAction:action to:target forEvent:event];
}
-(void)setIsClick:(BOOL)flag
{
isClick = flag;
}
@end
- 用RunTime来防止按钮被多次点击
- 防止按钮多次点击
- 防止按钮多次点击
- 防止按钮多次点击
- 前端防止按钮被多次点击
- Android防止按钮被多次点击
- 前端防止按钮被多次点击
- js 实现防止按钮被多次点击
- js 实现防止按钮被多次点击
- 防止用户多次点击按钮导致页面被多次提交
- .net防止多次点击按钮
- ios防止按钮多次点击
- android 防止按钮多次点击
- bcb 防止按钮多次点击
- 防止按钮多次点击方法
- android 防止 多次点击 重复点击按钮
- 防止按钮在短时间内被多次点击
- 防止按钮在短时间内被多次点击
- 23设计模式之外观模式(Facade)
- Google APAC 2016 University Graduates Test Round B解题报告
- 矩阵的一些简单操作
- 微信公众平台java开发详解(工程代码+解析)
- 32位进程注入64位进程
- 用RunTime来防止按钮被多次点击
- 【VBS脚本教程1】:写一个说话的语音程序
- Hibernate SessionFactory getCurrentSession is not valid without active transaction
- 【去哪儿】笔试
- redis实战之使用redis存储session数据
- Real-Time Rendering 笔记
- 脚本与嵌入式系统
- maven常见报错及解决方案
- Linux下的无名管道