禁用背景图像背后的视图的交互

时间:2014-09-03 13:31:55

标签: ios objective-c xcode touch user-interaction

为了显示帮助视图,我放了一个UIImageView后面有一些UIButtons。如何禁用这些按钮的用户交互?如果我触摸按钮后面的图像,它们会响应触摸事件。

图像代码背景:

self.helpBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.helpBackground.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
self.helpBackground.hidden = YES;
[self.view addSubview:self.helpBackground];

我使用过self.helpBackground.userInteractionEnabled = NO;但是没有工作。

感谢。

4 个答案:

答案 0 :(得分:0)

将按钮放在一个数组中并循环显示它们并禁用它们:

NSArray *buttonsArray = @[yourButton1, yourButton2];

for (UIButton *button in buttonsArray) {
button.enabled = NO;
}

如果您希望它们再次启用,请将enabled设置为YES

答案 1 :(得分:0)

为helpView(imageview)添加标记并添加以下代码

for (UIView *view in self.view.subviews) {
        if (view.tag != yourViewTag) {
            view.userInteractionEnabled = NO;
        }
    } 

删除帮助屏幕后,请使用以下代码

for (UIView *view in self.view.subviews) {
            view.userInteractionEnabled = YES;
    } 

答案 2 :(得分:0)

您可以尝试以下解决方案..当出现帮助imageview时

    for (UIView *view in [self.view subviews]) 
    {
        if (view isKindOfClass:[UIButton Class]) 
        {
            view.userInteractionEnabled = NO;
        }
       else
        {
            view.userInteractionEnabled = YES;
        }
    } 

////After dismissing the help screen you can

    for (UIView *view in [self.view subviews]) 
    {
        if (view isKindOfClass:[UIButton Class]) 
        {
            view.userInteractionEnabled = YES;
        }
    } 

////(或者)只需按照以下方式执行

self.view.userInteractionEnabled=YES;

希望它可以帮助你..

答案 3 :(得分:0)

您可以创建一个方法来禁用helpBackground下所有视图的用户交互:

- (void)disableUserInteractionForViewsUnderView:(UIView *)view
{
    CGRect area = view.frame;
    for (UIView *underView in [self.view subviews]) {
        if(CGRectContainsRect(area, underView.frame))
            underView.userInteractionEnabled = NO;
    }
}

之后,您可以在需要的地方拨打电话:

[self disableUserInteractionForViewsUnderView:self.helpBackground];

修改

我在github.com上创建了一个UIViewController类别要点。您可以在此处找到它:https://gist.github.com/alexcristea/0244b50e503e8bf4f25d

你可以像这样使用它:

[self enumerateViewsPlacedUnderView:self.helpBackground usingBlock:^(UIView *view) {
    if ([view isKindOfClass:[UIButton class]]) {
        view.userInteractionEnabled = NO;
    }
}];