ios - 创建模糊叠加 View

在新版 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 在完成后仍然可见呈现在上方。

https://stackoverflow.com/questions/17041669/

相关文章:

objective-c - 在 Objective-C 中为类定义私有(private)方法的最佳方

objective-c - 如何纯粹用 C 编写 iOS 应用程序

iphone - 按下时 UILongPressGestureRecognizer 被调用两次

objective-c - objectForKey 和 valueForKey 的区别?

objective-c - "sending ' 常量 NSString * ' to parame

iphone - 您在编写 Objective-C 和 Cocoa 时使用了哪些最佳实践?

objective-c - 如何在 Objective-C 中反转 NSArray?

objective-c - 如何使用 NSTimer?

iphone - 如何在 UIView 下绘制阴影?

ios - 如何删除 UITableView 中的空单元格?