简体   繁体   English

如何在当前视图上方创建半透明视图层?

[英]How do I make a semi-transparent view layer above a current view?

You've likely seen this before, its become exceedingly popular in consumery chic apps like ScoutMob. 您之前可能已经看过它,它在像ScoutMob这样的消费时尚应用中变得非常流行。 I'm trying to implement a 60% transparent view on launch that will cover my home screen, explaining how to use the functions of the home screen and disappears on tap. 我正试图在启动时实现60%透明视图,覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失。

I have my entire app completed (it's using .xib's since its from a years ago, but feel free to explain in storyboard format as well, since I will likely reuse this feature in other iOs 5.0+ applications.) 我完成了我的整个应用程序(它是从几年前开始使用.xib的,但是也可以用故事板格式来解释,因为我可能会在其他iOs 5.0+应用程序中重用此功能。)

I have no problem making single views, but temporarily layering one on top of another is something I haven't figured out intuitively. 我制作单个视图没有问题,但暂时将一个视图叠加在另一个上面是我没有直观地想出来的。 I'll continue researching and include any helpful tips I find in case other people are trying to do the same thing. 我将继续研究并包括我发现的任何有用的提示,以防其他人试图做同样的事情。

// get your window screen size
CGRect screenRect = [[UIScreen mainScreen] bounds];
//create a new view with the same size
UIView* coverView = [[UIView alloc] initWithFrame:screenRect];
// change the background color to black and the opacity to 0.6
coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
// add this new view to your main view
[self.view addSubview:coverView];

when you are done with it , you can remove it : 当你完成它,你可以删除它:

[coverView removeFromSuperview];

Swift3 version: Swift3版本:

// get your window screen size
let screenRect = UIScreen.main.bounds
//create a new view with the same size
let coverView = UIView(frame: screenRect)
// change the background color to black and the opacity to 0.6
coverView.backgroundColor = UIColor.black.withAlphaComponent(0.6)        
// add this new view to your main view
self.view.addSubview(coverView)

to remove it: 删除它:

coverView.removeFromSuperview()

This can be easily accomplished with a UITextView and a UIButton. 使用UITextView和UIButton可以轻松完成此操作。 Simply place the UITextView on the screen with the content you want to display, making it the full frame size of the screen, and change the background color to a black background with background alpha of .6 只需将UITextView放在屏幕上,其中包含您要显示的内容,使其成为屏幕的完整帧大小,并将背景颜色更改为背景alpha为.6的黑色背景。

[UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6];

Then place the button as a subview on top, making it the full frame of the screen, and setting it's alpha to 0. Set the action of the button to hide the textview and button. 然后将按钮作为子视图放在顶部,使其成为屏幕的完整框架,并将其alpha设置为0.设置按钮的操作以隐藏文本视图和按钮。

Example: 例:

UITextView* textview = [[UITextView alloc] initWithFrame:self.view.frame];
[textview setText: @"Text here"];
[textview setBackgroundColor: [UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6]];
[textview setTextColor: [UIColor whiteColor]];
[self.view addSubview: textview];

UIButton* btn = [[UIButton alloc] initWithFrame:self.view.frame];
[btn setAlpha:0];
[btn addTarget:self action:@selector(method) forEvent:UIControlEventTouchUpInside];
[self.view addSubview: btn];

You may want to check the addTarget: method; 您可能想要检查addTarget:方法; I'm not sure those are the correct parameters off the top of my head. 我不确定那些是我头顶的正确参数。

Just make a mechanism that figures out if it is the first launch of the app, then tells your main view controller to add a (probably image) view on top of the current view with 0.6 alpha 只需创建一个机制,确定它是否是应用程序的第一次启动,然后告诉主视图控制器在当前视图的顶部添加一个(可能是图像)视图,带有0.6 alpha

if (figureOutIfIsFirstLaunch)
{
    UIImageView *myOverlay = [...];
    myOverlay.frame = self.view.bounds;
    myOverlay.alpha = 0.6;
    [self.view addSubview:myOverlay];
} 
  1. create UIView , name it what you want ( dimBackgroundUIView ) then set the backgroundcolor as Black and set alpha to 0.1 or 0.2 or.... 创建UIView ,将其命名为你想要的( dimBackgroundUIView )然后将backgroundcolor设置为Black并将alpha设置为0.1或0.2或....
  2. add these 2 lines of code when you need to dim the background: [self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView]; 当你需要调暗背景时添加这两行代码: [self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView]; [self.view addSubview:dimBackgroundUIView]; [self addConstraintsForDimView];
  3. and add these 2 lines of code when you want to remove the dimming background: if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview]; 当你想要删除调光背景时添加这两行代码: if(dimBackgroundUIView) [dimBackgroundUIView removeFromSuperview];
  4. do not forgot to add the constraints(layouts) for dimBackgroundUIView . 不要忘记为dimBackgroundUIView添加约束(布局)。

check this: iOS Dim background when a view is shown 检查一下: 显示视图时的iOS暗淡背景

and do not forget to read the note below the code to understand what you have to do. 并且不要忘记阅读代码下面的注释,以了解您必须做什么。

Swift 4 update Swift 4更新

 view.isOpaque = false  

 view.backgroundColor = UIColor.black.withAlphaComponent(0.2)

This option available in storyboard . 故事板中提供此选项。 Make sure to uncheck the Opaque option 确保取消选中“不透明”选项 在此输入图像描述

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

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