您之前可能已经看到过这种情况,它在 ScoutMob 等消费时尚应用程序中变得非常流行。我正在尝试在启动时实现 60% 的透明视图,它将覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失。
我已经完成了我的整个应用程序(它从几年前开始使用 .xib,但也可以随意以故事板格式进行解释,因为我可能会在其他 iOs 5.0+ 应用程序中重用此功能。)
我制作单个视图没有问题,但是暂时将一个视图分层是我还没有直观地想到的事情。我将继续研究并包括我发现的任何有用的提示,以防其他人尝试做同样的事情。
您之前可能已经看到过这种情况,它在 ScoutMob 等消费时尚应用程序中变得非常流行。我正在尝试在启动时实现 60% 的透明视图,它将覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失。
我已经完成了我的整个应用程序(它从几年前开始使用 .xib,但也可以随意以故事板格式进行解释,因为我可能会在其他 iOs 5.0+ 应用程序中重用此功能。)
我制作单个视图没有问题,但是暂时将一个视图分层是我还没有直观地想到的事情。我将继续研究并包括我发现的任何有用的提示,以防其他人尝试做同样的事情。
// 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];
完成后,您可以将其删除:
[coverView removeFromSuperview];
斯威夫特3版本:
// 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)
删除它:
coverView.removeFromSuperview()
这可以通过 UITextView 和 UIButton 轻松完成。只需将 UITextView 与您要显示的内容放在屏幕上,使其成为屏幕的全帧大小,并将背景颜色更改为背景 alpha 为 0.6 的黑色背景
[UIColor colorWithRed: 0 withGreen: 0 withBlue: 0 withAlpha: .6];
然后将按钮作为子视图放在顶部,使其成为屏幕的全帧,并将其 alpha 设置为 0。设置按钮的操作以隐藏 textview 和按钮。
例子:
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];
您可能需要检查 addTarget: 方法;我不确定这些参数是否正确。
只需建立一个机制来确定它是否是应用程序的第一次启动,然后告诉您的主视图控制器在当前视图之上添加一个(可能是图像)视图,具有 0.6 alpha
if (figureOutIfIsFirstLaunch)
{
UIImageView *myOverlay = [...];
myOverlay.frame = self.view.bounds;
myOverlay.alpha = 0.6;
[self.view addSubview:myOverlay];
}
UIView,将其命名为您想要的 ( dimBackgroundUIView),然后将 backgroundcolor 设置为 Black 并将 alpha 设置为 0.1 或 0.2 或....[self.view addSubview:dimBackgroundUIView]; [self
addConstraintsForDimView];if(dimBackgroundUIView) [dimBackgroundUIView
removeFromSuperview];dimBackgroundUIView.检查这个: iOS Dim background when a view is shown
并且不要忘记阅读代码下方的注释以了解您必须做什么。