在新的iOS音乐应用中,我们可以看到一个模糊了的视图后面是一个专辑封面。如何实现这样的效果呢?我已经阅读了文档,但没有找到相关内容。 ![](https://istack.dev59.com/QXEg4m.webp)
![](https://istack.dev59.com/QXEg4m.webp)
您可以直接使用“Visual Effect View with Blur”和“Visual Effect View with Blur and Vibrancy”来制作背景模糊。
iOS应用程序中制作模糊背景的步骤如下:
苹果公司为UIImage类提供了一个名为UIImage+ImageEffects.h的扩展。在这个类中,您可以使用所需的方法来模糊您的视图。
如果为tableView添加一个深色模糊视图,这会让它看起来非常美观:
tableView.backgroundColor = .clear
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = tableView.bounds
blurEffectView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
// Assigning blurEffectView to backgroundView instead of addSubview to tableView makes tableView cell not blocked by blurEffectView
tableView.backgroundView = blurEffectView
Swift 4:
要添加覆盖层或弹出视图,您还可以使用容器视图,从中获取免费的视图控制器(您可以从常规对象调色板/库中获取容器视图)
步骤:
有一个视图(图片中的ViewForContainer),它包含此容器视图,在显示容器视图的内容时将其变暗。在第一个视图控制器内连接插座。
当第一个视图控制器加载时隐藏此视图
单击按钮时取消隐藏,输入图像描述
要在显示容器视图内容时使此视图变暗,请将视图背景设置为黑色,不透明度设置为30%
我已经在其他Stackoverflow问题中添加了关于popview视图创建的答案 https://dev59.com/-GQo5IYBdhLWcg3wOdFZ#49729431
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIView *subView = [[UIView alloc] initWithFrame:popupView.frame];
UIColor * backImgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_Img.png"]];
subView.backgroundColor = backImgColor;
subView.alpha = 0.5;
[mainView addSubview:subView];