利用Runtime来拦截UIButton的点击事件,防止重复点击
2017-03-27 16:29
543 查看
对于公司原来的一些代码,想对UIButton的点击事件做一部分修改,但是如果使用继承出来的UIBtton来解决的话, 又要改大量的代码,这时候,使用runtime拦截替换发送点击事件的方法可以迅速解决这个问题,超级给力!
不废话,直接上代码
不废话,直接上代码
#import <UIKit/UIKit.h> @interface UIControl (UIControl_XY) @property (nonatomic, assign) NSTimeInterval cjr_acceptEventInterval;// 可以用这个给重复点击加间隔 @end
#import "UIControl+UIControl_XY.h" #import <objc/runtime.h> @interface UIControl() @property (nonatomic, assign) NSTimeInterval cjr_acceptEventTime; @end @implementation UIControl (UIControl_XY) static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval"; static const char *UIControl_acceptEventTime = "UIControl_acceptEventTime"; - (NSTimeInterval )cjr_acceptEventInterval{ return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue]; } - (void)setCjr_acceptEventInterval:(NSTimeInterval)cjr_acceptEventInterval{ objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(cjr_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (NSTimeInterval )cjr_acceptEventTime{ return [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue]; } - (void)setCjr_acceptEventTime:(NSTimeInterval)cjr_acceptEventTime{ objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cjr_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } + (void)load{ //获取着两个方法 Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:)); SEL sysSEL = @selector(sendAction:to:forEvent:); Method myMethod = class_getInstanceMethod(self, @selector(cjr_sendAction:to:forEvent:)); SEL mySEL = @selector(cjr_sendAction:to:forEvent:); //添加方法进去 BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod)); //如果方法已经存在了 if (didAddMethod) { class_replaceMethod(self, mySEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod)); }else{ method_exchangeImplementations(systemMethod, myMethod); } //----------------以上主要是实现两个方法的互换,load是gcd的只shareinstance,果断保证执行一次 } - (void)cjr_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{ if (NSDate.date.timeIntervalSince1970 - self.cjr_acceptEventTime < self.cjr_acceptEventInterval) { return; } if (self.cjr_acceptEventInterval > 0) { self.cjr_acceptEventTime = NSDate.date.timeIntervalSince1970; } [self cjr_sendAction:action to:target forEvent:event]; } @end
@interface ALViewController () @property (nonatomic, strong) UIButton *; @end @implementation AutoLayoutViewController - (void)viewDidLoad { [super viewDidLoad]; self.suggessBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.suggessBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside]; self.suggessBtn.cjr_acceptEventInterval = 5.0f; } - (void)clickWithInterval:(UIButton *)suButton{ NSLog(@"打印出来--"): }
相关文章推荐
- iOS开发- 利用runtime拦截UIButton的点击事件,防止重复点击
- 利用runtime,避免UIButton 重复点击, 可变数组和可变字典为nil,或者数组越界导致的崩溃
- 防止 UIButton 重复点击,多次响应事件
- runtime - UIButton点击事件重复处理
- 利用runtime懒人实现iOS 防止按钮连续点击 连续响应事件,可以自己设置时间间隔,拖进工程中立即生效
- 利用 runtime,解决多次点击相同 button,导致重复跳转的问题-b
- iOS之使用Runtime解决UIButton多次点击(重复点击)
- iOS开发--利用 runtime,解决多次点击相同 button,导致重复跳转的问题
- 用Runtime解决UIButton重复点击问题
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- Runtime优雅的解决UIButton多次点击(重复点击)
- 【Android】Android防止过快点击造成多次事件执行(防止按钮重复点击)
- android防止重复点击事件的发生笔记
- iOS小技巧–用runtime 解决UIButton 重复点击问题
- 利用runtime,解决多次点击相同button,导致重复跳转的问题
- 防止android控件点击事件重复提交
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- iOS小技巧--用runtime 解决UIButton 重复点击问题
- iOS 防止UITableViewCell快速重复点击多次响应事件
- 利用runtime解决button重复响应事件