利用runtime,简单实现导航栏push页面的全屏返回

主要的实现思路就是:自定义导航栏,利用runtime运行时机制,获取系统的手势target 和action,然后将系统的手势加到导航控制器的视图,实现全屏返回;

OC操作如下:

1.自定义导航栏,导入运行时框架,遵守代理协议:UIGestureRecognizerDelegate

 #import 
 @interface MainNavVC :   UINavigationController

2.在 viewDidLoad 中实现如下代码:

 //    1.使用运行时,打印手势中的所有属性
//    unsigned int count = 0;
//    Ivar  * ivars  =  class_copyIvarList([UIGestureRecognizer class], &count);
//    for (int i = 0; i < count; i++) {
//        const char * name =  ivar_getName(ivars[i]);
//        NSLog(@"%s",name);
//    }

//    2.获取当前interactivePopGestureRecognizer 中的“ _targets” 数据,取第一个;
id targets =  [self.interactivePopGestureRecognizer valueForKey:@"_targets"];
id targetObjc = targets[0];
id target = [targetObjc valueForKey:@"target"];
SEL action = NSSelectorFromString(@"handleNavigationTransition:");
UIGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc]initWithTarget:target action:action];
[self.view addGestureRecognizer:panGes];

另一种写法:思想还是上面的思想

(1)获取系统自带滑动手势的target对象

id target = self.interactivePopGestureRecognizer.delegate;
// 创建全屏滑动手势,调用系统自带滑动手势的target的action方法
UIGestureRecognizer * apopRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
// 设置手势代理,拦截手势触发
apopRecognizer.delegate = self;
// 给导航控制器的view添加全屏滑动手势
[self.view addGestureRecognizer:apopRecognizer];
// 禁止使用系统自带的滑动手势
self.interactivePopGestureRecognizer.enabled = NO;

(2)本地实现 handleNavigationTransition 方法:

- (void)handleNavigationTransition:(UIPanGestureRecognizer *)recognizer {
//    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:recognizer];
//    [self performSelector:@selector(todoSomething:) withObject:recognizer afterDelay:0.2f];
//
/**
 * 稳定进度区间,让它在0.0(未完成)~1.0(已完成)之间
 */
    CGFloat progress = [recognizer translationInView:recognizer.view].x / recognizer.view.bounds.size.width;
    progress = MIN(1.0, MAX(0.0, progress));
    NSLog(@"progress ==  %f",progress);
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"UIGestureRecognizerStateBegan");
    }
    else if (recognizer.state == UIGestureRecognizerStatePossible) {
        NSLog(@"UIGestureRecognizerStatePossible");
    }
    else if (recognizer.state == UIGestureRecognizerStateFailed) {
        NSLog(@"UIGestureRecognizerStateFailed == " );
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state ==     UIGestureRecognizerStateCancelled) {
        NSLog(@"UIGestureRecognizerStateEnded");
        if (progress > 0.5) {
        } else {
        }
    }
}

swift操作如下:

override func viewDidLoad() {
super.viewDidLoad()

    //1.使用运行时,打印手势中所有的属性
    //        var count :UInt32 = 0
    //        let ivars =  class_copyIvarList(UIGestureRecognizer.self, &count)!
    //        for i in 0 ..< count {
    //            let namep = ivar_getName(ivars[Int(i)])!
    //            let name = String(cString:namep)
    //            print(name)
    //        }

    guard let targets = interactivePopGestureRecognizer!.value(forKey: "_targets") as? [NSObject] else {
        return
    }
    let targetObjc = targets[0]
    let target = targetObjc.value(forKey: "target")
    let action = Selector(("handleNavigationTransition:"))
    let panGes = UIPanGestureRecognizer(target: target, action: action)
    panGes.delegate = self
    view.addGestureRecognizer(panGes)

}

遵守代理协议,根控制器取消手势事件

extension HZNavigationController:UIGestureRecognizerDelegate{
      func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            // 注意:只有非根控制器才有滑动返回功能,根控制器没有。
           // 判断导航控制器是否只有一个子控制器,如果只有一个子控制器,肯定是根控制器
          if (self.childViewControllers.count == 1) {
              // 表示用户在根控制器界面,就不需要触发滑动手势,
              return false;
          }
          // 当前页面是显示结果页,不响应滑动手势
          let vc = self.childViewControllers.last!
          if vc.isKind(of: UIInputViewController.self){
              return false;
          }
          return true
  
      }

}

你可能感兴趣的:(利用runtime,简单实现导航栏push页面的全屏返回)