简体   繁体   English

iOS 显示视图时背景变暗

[英]iOS Dim background when a view is shown

In my main view, I do some gesture action causing some new view to be shown.在我的主视图中,我做了一些手势动作,导致显示一些新视图。 At this time I want to dim the entire background (except this new view) as a good UI practice.这时候我想把整个背景(除了这个新视图)调暗,作为一个好的 UI 实践。 In HTML, Javascript it looks like so - How do I get this same effect in iOS?在 HTML、Javascript 中看起来像这样 - 我如何在 iOS 中获得同样的效果?

在此处输入图像描述

Lay a UIView over the background, set its background to [UIColor blackColor] and set its opacity to like 0.6.在背景上放置一个 UIView,将其背景设置为[UIColor blackColor]并将其不透明度设置为 0.6。 Then add that UIView to the parent view.然后将 UIView 添加到父视图。

This'll dim out the background AND intercept any taps to background controls.这将使背景变暗并拦截对背景控件的任何点击。

While Dan's suggestion is on target, you cannot use a modal view controller in this case because a typical modal covers the entire screen, blocking the parent view controller, even if you have the background of your view as transparent (you'll see whatever you have in the application's UIWindow).虽然 Dan 的建议是正确的,但在这种情况下,您不能使用模态视图 controller,因为典型的模态会覆盖整个屏幕,从而阻止父视图 controller,即使您的视图背景是透明的(无论您看到什么在应用程序的 UIWindow 中有)。

If you are doing this on the iPad there are 2 modal presentation styles ( UIViewModalPresentationStylePageSheet and UIViewModalPresentationStyleFormSheet ) that can do something similar, but those will not work on an iPhone or iPod Touch.如果您在 iPad 上执行此操作,则有 2 个模态演示 styles( UIViewModalPresentationStylePageSheetUIViewModalPresentationStyleFormSheet )可以做类似的事情,但这些在 iPhone 或 iPod Touch 上不起作用。

Add the "shadow" view, with the dark background and partial opacity and whatever view you want to be in the foreground, to the view controller's view directly.将具有深色背景和部分不透明度的“阴影”视图以及您希望在前景中的任何视图直接添加到视图控制器的视图中。 You can animate them in using standard UIView animation blocks, or CoreAnimation.您可以使用标准 UIView animation 块或 CoreAnimation 为它们设置动画。

Another note, if you want to intercept touches to that shadow view you can either make it a giant button, subclass UIView to override one of the touch methods like touchesEnded: or change it to a UIControl which can receive touch events like a UIButton, but without the extra options for text, shadows, states etc.另请注意,如果您想拦截对该阴影视图的触摸,您可以将其设置为一个巨大的按钮,子类 UIView 以覆盖触摸方法之一,如touchesEnded:或将其更改为可以接收触摸事件的 UIControl,如 UIButton,但是没有文本、阴影、状态等的额外选项。

The above two answers work if the new view has it's own 'background', ie has no transparency.如果新视图有自己的“背景”,即没有透明度,则上述两个答案有效。 The problem that led me here cannot be solved that way though, what I was trying to do is this: I'm running an AVCaptureSession on my screen with the usual AVCaptureVideoPreviewLayer to display video in real time.但是,导致我来到这里的问题无法通过这种方式解决,我想做的是:我正在使用通常的AVCaptureSession在屏幕上运行AVCaptureVideoPreviewLayer以实时显示视频。 I want to select a part of the screen (to later do something with only this part), and when this part is selected want to dim the rest of the video preview. I want to select a part of the screen (to later do something with only this part), and when this part is selected want to dim the rest of the video preview. This is what it looks like:这是它的样子:

This is how I solved this: A new view is created depending on where the screen is touched and added as a subview of the video preview view.这就是我解决这个问题的方法:根据触摸屏幕的位置创建一个新视图,并将其添加为视频预览视图的子视图。 Then, I create 4 additional, non-overlapping rectangular subviews with the before-mentioned background color and opacity to cover the rest of the screen.然后,我用前面提到的背景颜色和不透明度创建了 4 个额外的、不重叠的矩形子视图,以覆盖屏幕的 rest。 I explicitly need all these views not to be subviews of the main view, because I need the ability to touch the screen somewhere else and change the selected area.我明确需要所有这些视图不是主视图的子视图,因为我需要能够在其他地方触摸屏幕并更改所选区域。

I'm sure there are more elegant ways to solve this, so if someone knows how please comment...我敢肯定有更优雅的方法来解决这个问题,所以如果有人知道如何请评论......

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
{
    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

note: you can optimise the code by using graphical interface on Xcode but i had to write code programmatically to be able to test it注意:您可以使用 Xcode 上的图形界面优化代码,但我必须以编程方式编写代码才能对其进行测试

so the idea is to:所以这个想法是:

  1. create UIView name it what you want (dimBackgroundUIView) than set the backgroundcolor as Black and set alfa to 0.1 or 0.2 or....创建 UIView 将其命名为您想要的名称(dimBackgroundUIView),而不是将背景颜色设置为黑色并将 alfa 设置为 0.1 或 0.2 或....
  2. add this 2 line of code when you need to dim the background:[self.view addSubview:dimBackgroundUIView];当您需要调暗背景时添加这 2 行代码:[self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView]; [self addConstraintsForDimView];
  3. and add this 2 line of code when you want to remove the dimming background: if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];并在要删除调光背景时添加以下 2 行代码: if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];

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

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