iOS显示视图时的暗淡背景

在我的主视图中,我做了一些手势动作,导致显示一些新的视图。 这时我想将整个背景(除了这个新视图)调暗为一个很好的UI练习。 在HTML中,Javascript看起来如此 – 我如何在iOS中获得相同的效果?

在此处输入图像描述

在背景上放置UIView,将其背景设置为[UIColor blackColor]并将其不透明度设置为0.6。 然后将该UIView添加到父视图中。

这将使背景变暗并拦截任何背景控件的水龙头。

虽然Dan的建议是目标,但在这种情况下你不能使用模态视图控制器,因为一个典型的模态覆盖整个屏幕,阻止父视图控制器,即使你的视图背景是透明的(你会看到任何你在应用程序的UIWindow中)。

如果您在iPad上执行此操作,则有两种模式演示样式( UIViewModalPresentationStylePageSheetUIViewModalPresentationStyleFormSheet )可以执行类似的操作,但这些样式UIViewModalPresentationStylePageSheet用于iPhone或iPod Touch。

将具有深色背景和部分不透明度以及您想要在前景中的任何视图的“阴影”视图直接添加到视图控制器的视图中。 您可以使用标准UIView动画块或CoreAnimation为它们设置动画。

另外需要注意的是,如果你想拦截那个阴影视图的触摸,你可以使它成为一个巨大的按钮,子类UIView覆盖触摸方法之一,如touchesEnded:或者将其改为UIControl,它可以接收像UIButton这样的触摸事件,但是没有文字,阴影,状态等的额外选项。

如果新视图具有自己的“背景”,即没有透明度,则上述两个答案都有效。 导致我在这里的问题无法通过这种方式解决,我试图做的是:我在屏幕上运行AVCaptureSession ,使用通常的AVCaptureVideoPreviewLayer实时显示video。 我想选择一部分屏幕(稍后只对此部分做一些事情),当选择此部分时,想要调暗video预览的其余部分。 这就是它的样子:

这就是我解决这个问题的方法:根据触摸屏幕的位置创建一个新视图,并将其添加为video预览视图的子视图。 然后,我创建了4个额外的,不重叠的矩形子视图,其中包含前面提到的背景颜色和不透明度,以覆盖屏幕的其余部分。 我明确要求所有这些视图不要成为主视图的子视图,因为我需要能够触摸其他地方的屏幕并更改所选区域。

我确信有更优雅的方法可以解决这个问题,所以如果有人知道如何评论…

 #import  @interface ViewController : UIViewController { UIView *mainView; UIView *dimBackgroundUIView; UIView *customView; UIButton *btn; } @end #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; mainView = [[UIView alloc] init]; mainView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:mainView]; [self addConstraintsForMainView]; btn = [[UIButton alloc] initWithFrame:CGRectMake(200, 200, 100, 30)]; [btn setTitle:@"Button" forState:UIControlStateNormal]; [btn setBackgroundColor:[UIColor blueColor]]; [btn addTarget:self action:@selector(showCustomView_Action:) forControlEvents:UIControlEventTouchUpInside]; [mainView addSubview:btn]; dimBackgroundUIView = [[UIView alloc] init]; dimBackgroundUIView.backgroundColor = [UIColor blackColor]; dimBackgroundUIView.alpha = 0.2; } -(void)removeCustomViewsFromSuperView { if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview]; if(customView) [customView removeFromSuperview]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)showCustomView_Action:(id)sender { customView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 80, 80)]; customView.backgroundColor = [UIColor redColor]; [self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView]; [self.view addSubview:customView]; UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeCustomViewsFromSuperView)]; tapped.delegate=self; tapped.numberOfTapsRequired = 1; [customView addGestureRecognizer:tapped]; } -(void) addConstraintsForDimView { [dimBackgroundUIView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:dimBackgroundUIView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; } -(void) addConstraintsForMainView { [mainView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:mainView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; } @end 

注意:您可以通过在Xcode上使用图形界面来优化代码,但我必须以编程方式编写代码才能测试它

所以我的想法是:

  1. 创建UIView命名它你想要的(dimBackgroundUIView)比将backgroundcolor设置为黑色并将alfa设置为0.1或0.2或….
  2. 当你需要调暗背景时添加这两行代码:[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
  3. 当你想删除调光背景时添加这两行代码:if(dimBackgroundUIView)[dimBackgroundUIView removeFromSuperview];