UIButton小技巧----点击事件时间间隔

来源:互联网 发布:改号软件 编辑:程序博客网 时间:2024/05/09 16:38

UIButton小技巧—-点击事件时间间隔

起因

      在开发过程中对于UIbutton的点击事件,如果进行频繁的点击,可能会造成事件的不必要的重复执行事件,甚至造成不必要的错误。

解决方案

     通过添加Category,重写sendAction:to:forEvent:方法。(通过runtime交换系统的sendAction:to:forEvent: 和 自定义的customSendAction:to:forEvent:方法,在定义方法中进行时间判断是否符合时间间隔需求,不符合直接return否则在调用系统的sendAction:to:forEvent:)。

代码

#import <UIKit/UIKit.h>@interface UIButton (TimeInterval)@property NSTimeInterval repeatEventInterval;@end
#import "UIButton+TimeInterval.h"#import <objc/runtime.h>const char *repeatEventIntervalKey  = "repeatEventIntervalKey";const char *previousClickTimeKey = "previousClickTimeKey";@implementation UIButton (TimeInterval)+ (void)load {    Method sendAction = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));    Method customSendAction = class_getInstanceMethod([self class], @selector(customSendAction:to:forEvent:));    method_exchangeImplementations(sendAction, customSendAction);}- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {    [super sendAction:action to:target forEvent:event];}- (void)setRepeatEventInterval:(NSTimeInterval)repeatEventInterval {    objc_setAssociatedObject(self, repeatEventIntervalKey, @(repeatEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSTimeInterval)repeatEventInterval {    return (NSTimeInterval)[objc_getAssociatedObject(self, repeatEventIntervalKey) doubleValue];}- (void)setPreviousClickTime:(NSTimeInterval)previousClickTime {    objc_setAssociatedObject(self, previousClickTimeKey, @(previousClickTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (NSTimeInterval)previousClickTime {    return [objc_getAssociatedObject(self, previousClickTimeKey) doubleValue];}- (void)customSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {    if ( NSDate.date.timeIntervalSince1970 - self.previousClickTime < self.repeatEventInterval ) {        return;    }    if (self.repeatEventInterval > 0) {        self.previousClickTime = NSDate.date.timeIntervalSince1970 ;    }    [self customSendAction:action to:target forEvent:event];}@end

应用

#import "UIButton+TimeInterval.h"button.repeatEventInterval = 2;
原创粉丝点击