uibutton + timecontrol

版权所有,转载请注明出处!

UIButton连续点击,时间间隔限制


.h

#import <UIKit/UIKit.h>

@interface UIButton (TimeControl)

/*事件响应时间间隔*/
@property (nonatomic,assign)  NSTimeInterval acceptEventInterval;
/*上一次执行的时间*/
@property (nonatomic,assign,readonly) NSTimeInterval lastCallTime;

@end

.m

#import "UIButton+TimeControl.h"
#import <objc/runtime.h>

@implementation UIButton (TimeControl)

+(void)load{
    Method before = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method after  = class_getInstanceMethod(self, @selector(xx_sendAction:to:forEvent:));
    method_exchangeImplementations(before, after);
}

-(void)xx_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if ([NSDate date].timeIntervalSince1970 - self.lastCallTime < self.acceptEventInterval) {
        return;
    }
    if (self.acceptEventInterval > 0) {
        self.lastCallTime = [NSDate date].timeIntervalSince1970;
    }
    [self xx_sendAction:action to:target forEvent:event];
}

static const char * UIControl_acceptEventInterval = "UIControl_accpetEventInterval";

- (NSTimeInterval)acceptEventInterval{
    return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}

- (void)setAcceptEventInterval:(NSTimeInterval)acceptEventInterval{
    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static const char * UIControl_lastCallTime = "UIControl_lastCallTime";

- (NSTimeInterval)lastCallTime{
    return  [objc_getAssociatedObject(self, UIControl_lastCallTime) doubleValue];
}

- (void)setLastCallTime:(NSTimeInterval)lastCallTime{
    objc_setAssociatedObject(self, UIControl_lastCallTime, @(lastCallTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end