拦截手势

2022-04-06  本文已影响0人  蓝天白云_Sam

拦截手势

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

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIGestureRecognizer (AE)

@end

NS_ASSUME_NONNULL_END

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

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

#import "UIGestureRecognizer+AE.h"
#import "NSObject+WY.h"
#import <objc/runtime.h>

@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
//   
//
//   
//   
//

#import <Foundation/Foundation.h>

#import <objc/runtime.h>

@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
//  WSAutoExpose
//
//  Created by SammyLan on 2019/6/27.
//  Copyright © 2019年 SammyLan. All rights reserved.
//

#import "NSObject+WY.h"
#import <objc/runtime.h>


@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

上一篇下一篇

猜你喜欢

热点阅读