gpt4 book ai didi

ios - 创建模糊叠加 View

转载 作者:IT老高 更新时间:2023-10-28 12:12:48 26 4
gpt4 key购买 nike

在新版 iOS 的音乐应用中,我们可以在模糊的 View 后面看到专辑封面。

这样的事情怎么可能完成?我已经阅读了文档,但没有找到任何内容。

最佳答案

你可以使用UIVisualEffectView来实现这个效果。这是一个 native API,已针对性能和电池生命周期进行了微调,而且易于实现。

swift :

//only apply the blur if the user hasn't disabled transparency effects
if !UIAccessibility.isReduceTransparencyEnabled {
view.backgroundColor = .clear

let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
//always fill the view
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
} else {
view.backgroundColor = .black
}

objective-C :

//only apply the blur if the user hasn't disabled transparency effects
if (!UIAccessibilityIsReduceTransparencyEnabled()) {
self.view.backgroundColor = [UIColor clearColor];

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//always fill the view
blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:blurEffectView]; //if you have more UIViews, use an insertSubview API to place it where needed
} else {
self.view.backgroundColor = [UIColor blackColor];
}

如果您以模态方式呈现此 View Controller 以模糊底层内容,则需要将模态呈现样式设置为 Over Current Context 并将背景颜色设置为清除,以确保底层 View Controller 在完成后仍然可见呈现在上方。

关于ios - 创建模糊叠加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17041669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com