繁体   English   中英

如何禁用UIView及其子视图中的轻击手势(所有交互)?

[英]How to disable Tap gesture (all interaction) in UIView and its subviews?

我有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。

userInteractionEnabled的属性适用于在其上设置属性的视图中的所有子视图。

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

因此,您应该能够...

divBoardView.userInteractionEnabled = NO;

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

然后...

divBoardView.userInteractionEnabled = YES;

将再次启用所有功能。

无需迭代[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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM