ifish/Ifish/Utinitys/IfishUtils/UIControl+YT.m

68 lines
1.7 KiB
Objective-C

//
// UIControl+YT.m
// CollectionViewTest
//
// Created by imac on 17/3/1.
// Copyright © 2017年 xiang. All rights reserved.
//
#import "UIControl+YT.h"
#import <objc/runtime.h>
@implementation UIControl (YT)
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const char *UIcontrol_ignoreEvent = "UIcontrol_ignoreEvent";
- (NSTimeInterval)yt_acceptEventInterval {
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setYt_acceptEventInterval:(NSTimeInterval)yt_acceptEventInterval {
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(yt_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)yt_ignoreEvent {
return [objc_getAssociatedObject(self, UIcontrol_ignoreEvent) boolValue];
}
- (void)setYt_ignoreEvent:(BOOL)yt_ignoreEvent {
objc_setAssociatedObject(self, UIcontrol_ignoreEvent, @(yt_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (void)load {
Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method b = class_getInstanceMethod(self, @selector(__yt_sendAction:to:forEvent:));
method_exchangeImplementations(a, b);
}
- (void)__yt_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if (self.yt_ignoreEvent) return;
if (self.yt_acceptEventInterval > 0) {
self.yt_ignoreEvent = YES;
[self performSelector:@selector(setYt_ignoreEvent:) withObject:@(NO) afterDelay:self.yt_acceptEventInterval];
}
[self __yt_sendAction:action to:target forEvent:event];
}
@end