码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 开发 右滑返回上一级控制器

时间:2018-01-15 20:38:29      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:代码   mst   设置   ace   rac   time   sele   oms   int   

#import <objc/runtime.h>

@interface UINavigationController (Transition)<UIGestureRecognizerDelegate>

 

- (void)transitionPanGestureDidLoad;

 

@end

 

@interface DLNavigationTransition ()

 

@end

 

@implementation DLNavigationTransition

 

/**

 *  启动右滑pop

 */

+ (void)enableNavigationTransitionWithPanGestureBack

{

 

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        Method viewDidLoadMethod = class_getInstanceMethod([UINavigationController class], @selector(viewDidLoad));

        Method transitionPanGestureDidLoadMethod = class_getInstanceMethod([UINavigationController class], @selector(transitionPanGestureDidLoad));

        

        method_exchangeImplementations(viewDidLoadMethod, transitionPanGestureDidLoadMethod);

        

    });

}

 

@end

 

 

@implementation UINavigationController (Transition)

 

- (void)transitionPanGestureDidLoad

{    

    if ([self isKindOfClass:[UINavigationController class]])

    {

        

        [self transitionPanGestureDidLoad];

 

        

        //1.获取系统interactivePopGestureRecognizer对象的target对象

        id target = self.interactivePopGestureRecognizer.delegate;

        //2.创建滑动手势,taregt设置interactivePopGestureRecognizer的target,所以当界面滑动的时候就会自动调用target的action方法。

        //handleNavigationTransition是私有类_UINavigationInteractiveTransition的方法,系统主要在这个方法里面实现动画的。

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];

        [pan addTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")];

        //3.设置代理

        pan.delegate = self;

        //4.添加到导航控制器的视图上

        [self.view addGestureRecognizer:pan];

        

        //5.禁用系统的滑动手势

        self.interactivePopGestureRecognizer.enabled = NO;

    }

}

 

#pragma mark - 滑动开始会触发

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

    UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;

    

    CGPoint translat = [pan translationInView:pan.view];

    if (translat.x < 0 ) {

        return NO;

    }

    //只有导航的根控制器不需要右滑的返回的功能。

    if (self.viewControllers.count <= 1)

    {

        

        return NO;

    }

    

    return YES;

}

 

@end

 //直接上代码  等你来拷贝

iOS 开发 右滑返回上一级控制器

标签:代码   mst   设置   ace   rac   time   sele   oms   int   

原文地址:https://www.cnblogs.com/tryFighting/p/8289324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!