iOS Runtim全局返回手势

前言:

          维护别人写的代码,增加的新需求,所有界面都要添加返回的手势,当我看到上个大师写的控制器代码,无话可说...  本身没有公共类,添加实在是特别费劲,网上也看了很多帖子,都说用Runtime 比较容易,找了篇写不的错的,给大家分享一下。          
正文:

#import "UINavigationController+ZWPanBack.h"
#import <objc/runtime.h>


@interface ZWFullScreenPopGestureRecognizerDelegate: NSObject <UIGestureRecognizerDelegate>
@property (nonatomic, weak) UINavigationController *navigationController;
@end

@implementation ZWFullScreenPopGestureRecognizerDelegate
//代理方法的实现
//在这里做手势识别判断
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    //判断是否是根控制器,如果是直接取消手势
    if (self.navigationController.viewControllers.count <= 1) {
        return NO;
    }
    //如果正在转场动画,取消手势
    if ([[self.navigationController valueForKey:@"_isTransitioning"] boolValue]) {
        return NO;
    }
    //判断手势方向,如果是从左往右拖动才开启手势
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    if (translation.x <= 0) {
        return NO;
    }
    
    return YES;
}
@end


@implementation UINavigationController (ZWPanBack)
//系统方法,加载时调用
+ (void)load {
    Method originalMethod = class_getInstanceMethod([self class], @selector(pushViewController:animated:));
    Method swizzledMethod = class_getInstanceMethod([self class], @selector(zw_pushViewController:animated:));
    //系统方法和自定义方法交换
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

//自定义方法和系统方法做交换
- (void)zw_pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    //寻找交互手势,如果交互手势没有被添加就添加交互手势。
    if (![self.interactivePopGestureRecognizer.view.gestureRecognizers containsObject:self.zw_popGestureRecognizer]) {
        [self.interactivePopGestureRecognizer.view addGestureRecognizer:self.zw_popGestureRecognizer];
        
        NSArray *targets = [self.interactivePopGestureRecognizer valueForKey:@"targets"];
        id internalTarget = [targets.firstObject valueForKey:@"target"];
        SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
        
        self.zw_popGestureRecognizer.delegate = [self zw_fullScreenPopGestureRecognizerDelegate];
        [self.zw_popGestureRecognizer addTarget:internalTarget action:internalAction];
        
        // 禁用系统的交互手势
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    if (![self.viewControllers containsObject:viewController]) {
        [self zw_pushViewController:viewController animated:animated];
    }
}

//自定义的交互手势
- (ZWFullScreenPopGestureRecognizerDelegate *)zw_fullScreenPopGestureRecognizerDelegate {
    //通过关联对象,直接将属性delegate添加到navigationController中
    ZWFullScreenPopGestureRecognizerDelegate *delegate = objc_getAssociatedObject(self, _cmd);
    if (!delegate) {
        delegate = [[ZWFullScreenPopGestureRecognizerDelegate alloc] init];
        delegate.navigationController = self;
        objc_setAssociatedObject(self, _cmd, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return delegate;
}


//通过关联对象添加属性 zw_popGestureRecognizer
- (UIPanGestureRecognizer *)zw_popGestureRecognizer {
    UIPanGestureRecognizer *panGestureRecognizer = objc_getAssociatedObject(self, _cmd);
    
    if (panGestureRecognizer == nil) {
        panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
        panGestureRecognizer.maximumNumberOfTouches = 1;
        
        objc_setAssociatedObject(self, _cmd, panGestureRecognizer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return panGestureRecognizer;
}


 当然这个写的是用Push跳转,使用pop道理其实也是一样的。到这里其实手势已经可以正常使用了.
 但是在A界面需要隐藏UINavigationController,B界面需要显示的时候,你会发现当你在B界面手势触发之后,A界面的viewWillAppear会直接调用。这时你写的self.navigationController.navigationBarHidden = YES ;就会触发,造成滑动时UINavigationController隐藏了。看图

这时 ,我们就不能使用self.navigationController.navigationBarHidden = YES; 

这是因为导航条是属于导航控制器的而并不是导航控制器的每个子VC都有一个属于自己的导航条。

而我实际想要的效果却是在手势滑动返回A界面途中导航条随着B界面一起偏移,如图


 那就是在A的viewWillAppear方法中不要使用self.navigationController.navigationBar.hidden = YES;这个方法而应该使用[self.navigationControllersetNavigationBarHidden:YESanimated:YES]这个方法,相应的在B的viewWillAppear方法中也不要使用self.navigationController.navigationBar.hidden = NO这个方法而应该使用[self.navigationControllersetNavigationBarHidden:NOanimated:YES]这个方法。注意:animated这个参数一定要设置为YES,因为使用[self.navigationController setNavigationBarHidden:YES animated:YES]之所以能达到上图这种我们想要的效果就是因为有这个动画,而这个动画效果就是导航条随着导航控制器的子VC的界面一起偏移。

猜你喜欢

转载自blog.csdn.net/weixin_43431337/article/details/85157458