拦截手势

拦截手势

  • UIGestureRecognizer+AE.h
//
//  UIGestureRecognizer+AE.h
// 
//
// 
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface UIGestureRecognizer (AE)

@end

NS_ASSUME_NONNULL_END

  • UIGestureRecognizer+AE.m
//
//  UIGestureRecognizer+AE.m
//   
//
//   
//

//
//  UIGestureRecognizer+AE.m
//  WSAutoExpose
//
//  Created by Sammy Lan on 2022/4/6.
//

#import "UIGestureRecognizer+AE.h"
#import "NSObject+WY.h"
#import 

@implementation UIGestureRecognizer (AE)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self wsa_replaceSelector:@selector(addTarget:action:)
                         forClass:[UIGestureRecognizer class]
                       bySelector:@selector(wsa_addTarget:action:)];
        
        [self wsa_replaceSelector:@selector(initWithTarget:action:)
                         forClass:[UIGestureRecognizer class]
                       bySelector:@selector(initWithTarget_wsa:action:)];
        
        
    });
}


#pragma mark - 手势点击拦截
-(instancetype) initWithTarget_wsa:(id)target action:(SEL)action{
    self = [self initWithTarget_wsa:target action:action];
    if(target == nil || action == nil) {
        return self;
    }

    [self wsa_handleTarget:target action:action];
    return self;
    
}

-(void)wsa_addTarget:(id)target action:(SEL)action
{
    [self wsa_addTarget:target action:action];
    [self wsa_handleTarget:target action:action];
    
}

- (void)wsa_handleTarget:(id)target action:(SEL)action
{
    if(target == nil) {
        return;
    }
    if (![self isKindOfClass:[UITapGestureRecognizer class]]) {
        return;
    }
    
    Class targetClass = [target class];
    NSString * newMethodName = [NSString stringWithFormat:@"wsa_%@",NSStringFromSelector(action)];
    SEL newSelector = NSSelectorFromString(newMethodName);
    
    Method newMethod = class_getInstanceMethod(targetClass, newSelector);
    if (newMethod != nil) {
        return;
    }
    newMethod = class_getInstanceMethod([self class], @selector(wsa_respondActionForGestureRecognize:));
    [NSObject wsa_replaceSelector:action forClass:targetClass bySelector:newSelector byMethod:newMethod];
}

- (void)wsa_respondActionForGestureRecognize:(UIGestureRecognizer*)gestureRecognize
{
    NSString * newMethodName = [NSString stringWithFormat:@"wsa_%@",NSStringFromSelector(_cmd)];
    SEL sel =NSSelectorFromString(newMethodName);
    if ([self respondsToSelector:sel]) {
        IMP imp = [self methodForSelector:sel];
        void(*func)(id,SEL,id) = (void*)imp;
        func(self,sel,gestureRecognize);
    }
}
@end

拦截实现

  • NSObject+WY.h
//
//  NSObject+WY.h
//   
//
//   
//   
//

#import 

#import 

@interface NSObject (WY)
+ (void)wsa_replaceSelector:(SEL)selector bySelector:(SEL)newSelector;
+ (void)wsa_replaceInstanceSelector:(SEL)selector bySelector:(SEL)newSelector;
+ (void)wsa_replaceSelector:(SEL)selector forClass:(Class)originalClass bySelector:(SEL)newSelector;
+ (void)wsa_replaceSelector:(SEL)selector forClass:(Class)originalClass bySelector:(SEL)newSelector byClass:(Class)byClass;

+ (void)wsa_replaceSelector:(SEL)selector forClass:(Class)originalClass bySelector:(SEL)newSelector byMethod:(Method)byMethod;

@end

  • NSObject+WY.m
//
//  NSObject+WY.m
//  WSAutoExpose
//
//  Created by SammyLan on 2019/6/27.
//  Copyright © 2019年 SammyLan. All rights reserved.
//

#import "NSObject+WY.h"
#import 


@implementation NSObject (WY)

+ (void)wsa_replaceSelector:(SEL)selector bySelector:(SEL)newSelector
{
    [self wsa_replaceSelector:selector forClass:[self class] bySelector:newSelector byClass:[self class]];
}

+ (void)wsa_replaceInstanceSelector:(SEL)selector bySelector:(SEL)newSelector
{
    Class c = object_getClass((id)self);
    [self wsa_replaceSelector:selector forClass:c bySelector:newSelector byClass:c];
}

+ (void)wsa_replaceSelector:(SEL)selector forClass:(Class)originalClass bySelector:(SEL)newSelector
{
    [self wsa_replaceSelector:selector forClass:originalClass bySelector:newSelector byClass:[self class]];
}

+ (void)wsa_replaceSelector:(SEL)selector forClass:(Class)originalClass bySelector:(SEL)newSelector byClass:(Class)byClass
{
    Method method = class_getInstanceMethod(originalClass, selector);
    Method newMethod = class_getInstanceMethod(byClass, newSelector);
    if (originalClass != byClass) {
        BOOL bAdd = class_addMethod(originalClass, newSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
        if (!bAdd) {
            NSCAssert(NO, @"AddFailed");
            return;
        }
        newMethod = class_getInstanceMethod(originalClass, newSelector);
    }
    method_exchangeImplementations(method, newMethod);
}

+ (void)wsa_replaceSelector:(SEL)selector forClass:(Class)originalClass bySelector:(SEL)newSelector byMethod:(Method)byMethod
{
    Method orignalMethod = class_getInstanceMethod(originalClass, selector);
    BOOL add = class_addMethod(originalClass, newSelector, method_getImplementation(byMethod), method_getTypeEncoding(byMethod));
    if (!add) {
        NSCAssert(NO, @"AddFailed");
        return;
    }
    Method newMethod = class_getInstanceMethod(originalClass, newSelector);
    method_exchangeImplementations(orignalMethod, newMethod);
}
@end

你可能感兴趣的:(拦截手势)