【问题标题】:How to disable Tap gesture (all interaction) in UIView and its subviews?如何在 UIView 及其子视图中禁用点击手势(所有交互)?
【发布时间】:2014-09-11 15:45:50
【问题描述】:

我有 2 个类 cellView(基本上是一个带有水龙头的单元格)和具有 64 个 cellViews 的板视图。 从我的 VC 中,我想在某些事件上禁用/启用它们的所有交互。 这是我的代码。

if(currentGame.isYourTurn==NO){
    [divBoardView  setUserInteractionEnabled:NO];
    for(UIView*currentView in  [divBoardView  subviews]){
        [currentView  setUserInteractionEnabled:NO];
    }
} else ..

在单元格视图中只是用

查看
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
   initWithTarget:self action:@selector(cellTapped)];
[self addGestureRecognizer:tapRecognizer];

我想在不添加和删除 tapGestureRecognisers 的情况下禁用和启用交互。

【问题讨论】:

    标签: ios objective-c uiview tap interaction


    【解决方案1】:

    userInteractionEnabled 的属性适用于您设置该属性的视图中的所有子视图。

    与点击手势识别器相同。如果您添加识别器并设置userInteractionEnabled = NO,则识别器将永远不会被触发。

    所以,你应该可以做到……

    divBoardView.userInteractionEnabled = NO;
    

    这将在所有子视图中递归工作(不是真的,但它会产生相同的效果)并且还会禁用任何识别器。

    那么……

    divBoardView.userInteractionEnabled = YES;
    

    将再次启用一切。

    【讨论】:

    • 天哪,谢谢。我发现了我的蹩脚的错误。我在 boardView 的 init 之前调用了这个。这段代码完美地工作
    • 哈哈!不用担心。乐于助人。
    【解决方案2】:

    无需迭代[divBoardView subviews] 并设置每个人的userInteractionEnabled 属性。只需在父视图上设置此属性,在这种情况下divBoardView 将禁用所有子视图的交互。

    话虽如此,UITapGestureRecognizer 不应附加到 divBoardView 或您计划禁用的任何视图,因为如果将 userInteractionEnabled 设置为 NO,它将不会触发。

    根据您的设计,最好将手势识别器附加到 viewController 的 view 属性:

    // Where self is the view controller
    [self.view addGestureRecognizer:tapRecognizer];
    

    以及基于某种状态的进程交互:

    @interface YourViewController ()
    
    @property (assign, nonatomic) BOOL isBoardActive;
    
    @end
    
    
    
    @implementation 
    
    - (void)handlerTap:(UITapGestureRecognizer *)tap {
    
        UIView *viewControllerView = tap.view.
        CGPoint location           = [tap locationInView:viewControllerView];
    
        if (_isBoardActive && CGRectContainsPoint(_boardView.frame, location)) {
            // Process tap on board   
        } else {
            // Process tap elsewhere
        }
    }
    
    @end
    

    这只是一种解决方案。很难推荐理想的解决方案,因为关于您的问题的信息很少。完成同一件事的方法还有很多,最佳选择取决于您当前的应用结构、设计等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      相关资源
      最近更新 更多