iOS暴力禁用navigationviewcontroller右滑手势和手势的优先级

1 : UINavigationController push出来的ViewController ,右滑时会pop回到之前的控制器;

     多数的情况下是丰富了用户体验,但是有时候我们不需要这种“体验”,需要禁用右滑返回的手势,关于禁用这个手势iOS中给出了相关方法,但是这些方法不起作用的情况时有发生。

    如果已经尝试过一些方法都不能够禁用右滑的手势,这里还有一个方法 (直接改变右滑手势本身);

   在要禁用右滑手势的ViewController的ViewDidLoad中 改变pan手势的响应事件

/// 禁用右滑返回手势
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];

   可以将pan手势的响应事件置nil,或者去做别的响应操作;

2 : 手势的优先级设置

   例如上面的禁用pan手势,如果你想要同时在此时响应swipe事件,这时候是不能响应swipe手势的。

  这时候我们可以设置让swipe的手势的优先级高于pan

/// 禁用右滑返回手势
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
    // 添加滑动手势
    /**  轻扫  */
    UISwipeGestureRecognizer * swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];
    
    UISwipeGestureRecognizer * swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipeRight];
    // 手势的优先级 优先响应swipe手势
    [pan requireGestureRecognizerToFail:swipeLeft];
    [pan requireGestureRecognizerToFail:swipeRight];

手势优先级方法解释:

[pan requireGestureRecognizerToFail: swipe];

如果swipe手势触发失败,这时候再响应pan 手势

原文地址:https://www.cnblogs.com/code-Officer/p/6771272.html