Files
featherVoice/QXLive/Tools/Category/UIControl+QX.m
2025-08-08 10:49:36 +08:00

87 lines
2.5 KiB
Objective-C

//
// UIControl+QX.m
// QXLive
//
// Created by 启星 on 2025/5/19.
//
#import "UIControl+QX.h"
static inline void swizzling_exchangeMethodWithSelector(Class clazz, SEL originalSelector, SEL exchangeSelector) {
// 获取原方法
Method originalMethod = class_getInstanceMethod(clazz, originalSelector);
// 获取需要交换的方法
Method exchangeMethod = class_getInstanceMethod(clazz, exchangeSelector);
if (!originalMethod || !exchangeMethod) {
return;
}
if (class_addMethod(clazz, originalSelector, method_getImplementation(exchangeMethod), method_getTypeEncoding(exchangeMethod))) {
class_replaceMethod(clazz, exchangeSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, exchangeMethod);
}
}
static inline void swizzling_exchangeMethodWithMethod(Method originalMethod, Method exchangeMethod) {
method_exchangeImplementations(originalMethod, exchangeMethod);
}
@implementation UIControl (QX)
- (NSTimeInterval)needEventInterval
{
return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setNeedEventInterval:(NSTimeInterval)needEventInterval
{
objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(needEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)ignoreEvent
{
return [objc_getAssociatedObject(self, BandNameKey) boolValue];
}
- (void)setIgnoreEvent:(BOOL)ignoreEvent
{
objc_setAssociatedObject(self, BandNameKey, [NSNumber numberWithBool:ignoreEvent], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+(void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
swizzling_exchangeMethodWithSelector([self class],
@selector(sendAction:to:forEvent:),
@selector(__uxy_sendAction:to:forEvent:));
});
}
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if (self.ignoreEvent) return; /*当ignoreEvent为no时, 此时会检测时间响应*/
if (self.needEventInterval > 0)
{
self.ignoreEvent = YES;
[self performSelector:@selector(changeIgnoreEvent) withObject:@(NO) afterDelay:self.needEventInterval];
}
[self __uxy_sendAction:action to:target forEvent:event]; //此时交换走原来方法;
}
- (void)changeIgnoreEvent
{
self.ignoreEvent = NO;
}
@end