美文网首页
全屏右滑返回

全屏右滑返回

作者: Heikki_ | 来源:发表于2016-11-21 21:23 被阅读68次
全屏返回.gif

以上全屏返回效果只需要在 UINavigationController 中写入下面代码

- (void)viewDidLoad {
    [super viewDidLoad];
    
    id target = self.interactivePopGestureRecognizer.delegate;
    
    SEL handler = NSSelectorFromString(@"handleNavigationTransition:");
    //  获取添加系统边缘触发手势的View
    UIView *targetView = self.interactivePopGestureRecognizer.view;
    
    //  创建pan手势 作用范围是全屏
    UIPanGestureRecognizer * fullScreenGes = [[UIPanGestureRecognizer alloc]initWithTarget:target action:handler];
    fullScreenGes.delegate = self;
    [targetView addGestureRecognizer:fullScreenGes];
    
    // 关闭边缘触发手势 防止和原有边缘手势冲突
    [self.interactivePopGestureRecognizer setEnabled:NO];
}

//  防止导航控制器只有一个rootViewcontroller时触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return self.childViewControllers.count == 1 ? NO : YES;
}

代码分析

id target = self.interactivePopGestureRecognizer.delegate;

这句代码目的是获取事件处理对象.以便自己添加的手势可以把事件处理委托给它.

SEL handler = NSSelectorFromString(@"handleNavigationTransition:");

这句就是获取委托对象里的处理方法.

UIPanGestureRecognizer * fullScreenGes = [[UIPanGestureRecognizer alloc]initWithTarget:target action:handler]; fullScreenGes.delegate = self; [targetView addGestureRecognizer:fullScreenGes];

这几句就是添加自己的全屏手势,通过目标-动作模式把任务交给了系统委托对象处理.

或者把以上代码写到UINavigationController分类中;
如果需要自定制导航时,实现是写在UINavigationController子类中,比较方便.如果不需要,可以单独写一个分类.这里写在GLNavigationController中,其中GLNavigationController.h继承自UINavigationController.

https://github.com/SherlockQi/NEScreenBack

代码:GitHub

文/Mrshang110(简书作者)
原文链接:http://www.jianshu.com/p/2e8d332c60ff
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

相关文章

网友评论

      本文标题:全屏右滑返回

      本文链接:https://www.haomeiwen.com/subject/mcoupttx.html