iOS 实现Navigation导航控制器全屏侧滑返回效果并监听侧滑的偏移量

关于全屏侧滑的效果可以参考《8行代码教你搞定导航控制器全屏滑动返回效果》上面说的很详细,我实现的监听侧滑偏移量就是在其基础上改造实现的。http://www.cocoachina.com/ios/20150811/12897.html
原创文章,欢迎转载.转载请注明出处: https://www.jianshu.com/p/7edf4493cc3b

实现的效果:

demo.gif

代码:

@interface NavigationViewController ()

//记录自定义手势的开始位置
@property (nonatomic, assign) CGFloat startX;

@end

@implementation NavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //自定义滑动手势
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(myHandleNavigationTransition:)];
    //将自定义手势添加到NavigationController的view上
    [self.view addGestureRecognizer:panGestureRecognizer];
    //设置自定义手势的代理,用于拦截自定义手势的触发
    panGestureRecognizer.delegate = self;
    //关闭系统的边缘手势
    self.interactivePopGestureRecognizer.enabled = NO;
}

- (void)myHandleNavigationTransition:(UIPanGestureRecognizer *)panGesture{
    //调用系统手势绑定的方法
    [self.interactivePopGestureRecognizer.delegate performSelector:@selector(handleNavigationTransition:) withObject:panGesture];
    //计算偏移量
    CGFloat offetX = [panGesture locationInView:self.view].x - self.startX;
    if (offetX >= 0) {
        //输出偏移量
        NSLog(@"%f", offetX);
    } else {
        NSLog(@"%f", 0.0f);
    }
}

//拦截自定义手势的触发,并记录触发手势的开始位置
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    //导航控制器的跟控制器不需要返回侧滑手势(如果不屏蔽会有奇怪的效果)
    if (self.childViewControllers.count == 1) {
        return NO;
    }
    //记录触发手势的开始位置
    self.startX = [gestureRecognizer locationInView:self.view].x;
    return YES;
}

Demo下载链接:
https://github.com/w-wh/WHPanGestureDemo

你可能感兴趣的:(iOS 实现Navigation导航控制器全屏侧滑返回效果并监听侧滑的偏移量)