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的界面一起偏移。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS Runtime是一种运行时环境,它是iOS操作系统中的重要组成部分。iOS Runtime为开发者提供了一套动态询问、修改和扩展应用程序行为的机制。 首先,iOS Runtime实现了Objective-C语言的动态特性。Objective-C是一种面向对象的编程语言,它具有动态特性,即在运行时能够动态地修改对象的行为。iOS Runtime允许开发者通过运行时系统对类、对象、方法以及属性进行动态操作。例如,开发者可以在运行时为某个类添加新的方法,也可以通过运行时修改类的实例变量。 其次,iOS Runtime还提供了消息发送机制。在Objective-C中,对象之间的通信是通过消息发送来完成的。iOS Runtime负责将消息转发给正确的接收者,并执行相应的方法。这个过程是动态的,开发者可以在运行时动态改变消息的接收者或方法的实现。这为Objective-C语言带来了很高的灵活性。 此外,iOS Runtime还支持方法的交换与替换。开发者可以通过运行时机制,在运行时将一个方法的实现替换为另一个方法的实现,或者交换两个方法的实现。这对于调试、性能监控和代码扩展都是非常有用的。 总结来说,iOS Runtime是iOS操作系统中实现Objective-C语言特性的核心组件。它提供了动态特性的支持,使开发者能够在运行时动态地修改类、对象和方法的行为,实现更加灵活和可扩展的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值