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

问题描述:

显示帮助视图我已经把UIImageView放在后面有一些UIButtons。我如何禁用这些按钮的用户交互?如果我触摸此图像的按钮位于后面,它们会响应触摸事件。禁用背景图像背后的视图交互

CODE FOR IMAGE背景:

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;但没有工作。

谢谢。

+0

你需要禁用用户交互的所有按钮不helpBackground ImageView的..! – Vidhyanand 2014-09-03 13:34:50

+0

当helpBackground打开时,您可以将所有按钮设置为button.hidden = YES。 – 2014-09-03 13:37:28

把你的按钮的阵列和循环通过他们,并禁用它们:

NSArray *buttonsArray = @[yourButton1, yourButton2]; 

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

当你希望他们再次启用只是环和设置enabledYES

保持标签,就可以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; 
    } 

你可以试试下面solution..When帮助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; 
     } 
    } 

////(OR)简单地做如下

self.view.userInteractionEnabled=YES; 

希望它可以帮助你..

您可以创建禁用用户交互的方法,帮助您处理帮助下的所有视图背景:

- (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; 
    } 
}];