实现苹果系统自带的侧滑返回上一级效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lausen34/article/details/52577530

最近在公司老项目上更改,准备做版本迭代,发现此版本不能实现侧滑返回上一级效果,
最后在navigationController里面找到了突破口

@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

可以看到这个属性里面带有pop关键字
逐字翻译过来,就是交互式的返回手势识别器(见笑了)

设置当前控制器的导航控制器的这个属性为YES并且设置代理

self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;

最后实现两个代理方法即可

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];

}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

如果想让所有的控制器都有这个效果,可以自定义一个控制器在自定义的控制器里实现这些方法,代理,等等,让其他的控制器继承自这个控制器就可以了

#import "LAUGlobalViewController.h"

@interface LAUGlobalViewController ()<UIGestureRecognizerDelegate>

@end

@implementation LAUGlobalViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];

}



- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

@end
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页