【问题标题】:How to disable back gesture in iOS 7 for only one view如何仅在一个视图中禁用 iOS 7 中的后退手势
【发布时间】:2013-10-01 05:27:50
【问题描述】:

我正在尝试使用以下代码集禁用视图控制器的后退手势。

FirstViewController.m 中,我正在设置interactivePopGestureRecognizer 的代表

- (void) viewWillLoad {

    // Other stuff..
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

然后实现<UIGestureRecognizerDelegate>方法并返回NO

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

     return NO;
}

在 dealloc 中,我将委托设置为 nil。 (我在某处读到,在 iOS 7 中,您必须手动将代表设置为 nil)

- (void)dealloc {

    self.navigationController.delegate = nil;
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

这适用于FirstViewController。但是当我将SecondViewController 推到这里时,手势也不起作用。如何仅在 FirstViewController 中禁用手势?

另外,当我弹出FirstViewController 以转到RootViewController,然后再次尝试推送FirstViewController 时,我得到对象释放错误:

[FirstViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 0x14ed0280

除了将代表设置为 nil 之外,我还需要做什么?还是我放错地方了?

【问题讨论】:

    标签: ios7 gesture back


    【解决方案1】:

    在您的 FirstViewController 中尝试以下未经测试的代码:

    -(void) viewWillAppear:(BOOL)animated 
    {
        [super viewWillAppear:animated];
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    -(void) viewWillDisappear:(BOOL)animated 
    {
        [super viewWillDisappear:animated];
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    

    【讨论】:

    • 添加此代码,当我尝试做一个后退手势时,禁用 SecondViewController 内所有元素的交互。为什么会这样??
    • Dude self.navigationController.interactivePopGestureRecognizer.enabled = NO; 对我来说很好,尝试删除代表。
    • 对不起,我忘记了一个回合,我更新了答案。
    • 谢谢,interactivePopGestureRecognizer.enabled 成功了。但它在我的 FirstViewController 中不起作用。好像还有别的问题。嗯..
    • 你确定你正在实现 view>Will
    【解决方案2】:

    我最初将这些答案放在已接受答案下方的评论中,但我觉得需要将其作为答案来获得更多可见度。

    通常情况下,您会发现接受的答案不起作用。这是因为viewWillAppear: 可以在视图添加到导航控制器的视图层次结构之前被调用,所以self.navigationController 将是nil。因此,在某些情况下可能不会禁用 interactivePopGestureRecognizer。你最好改为在viewDidAppear: 中调用它。

    下面是可以工作的代码(假设您的视图控制器已正确添加到导航控制器的视图层次结构中):

    目标-C

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [[[self navigationController] interactivePopGestureRecognizer] setEnabled:NO];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[[self navigationController] interactivePopGestureRecognizer] setEnabled:YES];
    }
    

    斯威夫特

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    

    【讨论】:

    • 你是个巫师约翰
    • 这应该是公认的答案。 iOS 12,迅捷 4.2
    【解决方案3】:

    我尝试了以上所有方法,但它们对我不起作用。所以我尝试了这个,它在 IOS7 和 IOS8 上都适用。

    只要确保你的视图控制器实现了这个协议,即 UIGestureRecognizerDelegate 并编写下面给出的代码。

    -(void)viewWillAppear : (BOOL) 动画 {

    [super viewWillAppear : animated];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    
        self.navigationController.interactivePopGestureRecognizer.enabled =
    

    没有;
    self.navigationController.interactivePopGestureRecognizer.delegate = 自己;
    }

    }

    -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {
    
        return NO;
    
    } else {
    
        return YES;
    
    }
    

    }

    【讨论】:

      【解决方案4】:

      我发现仅将手势设置为禁用并不总是有效。它确实有效,但对我来说,它只有在我使用过一次后退手势后才有效。第二次它不会触发背景手势。此外,正如 John Rogers 所说,使用 viewDidAppear 和 viewWillAppear 作为 navigationController 是很重要的,否则将是 nil。

      对我来说,解决方法是委托手势并实现 shouldbegin 方法以返回 NO:

      - (void)viewDidAppear:(BOOL)animated
      {
          [super viewDidAppear:animated];
      
          // Disable iOS 7 back gesture
          if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
              self.navigationController.interactivePopGestureRecognizer.enabled = NO;
              self.navigationController.interactivePopGestureRecognizer.delegate = self;
          }
      }
      
      - (void)viewWillDisappear:(BOOL)animated
      {
          [super viewWillDisappear:animated];
      
          // Enable iOS 7 back gesture
          if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
              self.navigationController.interactivePopGestureRecognizer.enabled = YES;
              self.navigationController.interactivePopGestureRecognizer.delegate = nil;
          }
      }
      
      - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
      {
          return NO;
      }
      

      【讨论】:

        【解决方案5】:

        这在 xCode 7 中对我有用:

        override func viewDidAppear(animated: Bool) {
                super.viewDidAppear(animated)
                self.navigationController!.interactivePopGestureRecognizer!.enabled = false
        
        }
        override func viewWillDisappear(animated: Bool) {
            super.viewDidDisappear(animated)
            self.navigationController!.interactivePopGestureRecognizer!.enabled = true
        }
        

        【讨论】:

          【解决方案6】:

          只有一个视图,我不知道方法...但是我使用下一个代码完全禁用滑动手势:

          在您的 AppDelegate.m 中

          if ([[UIDevice currentDevice].systemVersion floatValue] >= 7){
                  self.navigationController.interactivePopGestureRecognizer.enabled = NO;
              }
          

          【讨论】:

            猜你喜欢
            • 2013-06-17
            • 2021-08-21
            • 2014-06-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多