创建一个模糊重叠视图 [英] Creating a blurring overlay view

查看:80
本文介绍了创建一个模糊重叠视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新iOS的音乐应用程序中,我们可以在视图背后看到专辑封面,使其模糊。

如何才能完成这样的事情?我已阅读文档,但没有找到任何内容。



解决方案

您可以使用 UIVisualEffectView 影响。这是一个本机API,它已经过性能优化和电池续航时间的优化,并且易于实现。



Swift

  //仅当用户未禁用透明效果时才应用模糊
if!UIAccessibilityIsReduceTransparencyEnabled(){
view.backgroundColor = .clear

let blurEffect = UIBlurEffect(style:.dark)
let blurEffectView = UIVisualEffectView(effect:blurEffect)
//总是填充视图
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth,.flexibleHeight]

view.addSubview(blurEffectView)//如果您有更多UIViews,请使用insertSubview API将它放在需要的位置
} else {
view.backgroundColor = .black
}

Objective-C:

  //仅应用blur if (!U)用户没有禁用透明效果
IAccessibilityIsReduceTransparencyEnabled()){
self.view.backgroundColor = [UIColor clearColor];

UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView * blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//总是填充视图
blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:blurEffectView]; //如果你有更多的UIViews,可以使用insertSubview API把它放在需要的地方
} else {
self.view.backgroundColor = [UIColor blackColor];



$ b

如果您以模态方式呈现此视图控制器以模糊底层内容,我们需要将模式表示风格设置为Over Current Context,并设置背景颜色以确保底层视图控制器在超出时保持可见状态。


In the Music app of the new iOS, we can see an album cover behind a view that blurs it.

How can something like that be accomplished? I've read the documentation, but did not find anything there.

解决方案

You can use UIVisualEffectView to achieve this effect. This is a native API that has been fine-tuned for performance and great battery life, plus it's easy to implement.

Swift:

//only apply the blur if the user hasn't disabled transparency effects
if !UIAccessibilityIsReduceTransparencyEnabled() {
    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];
}

If you are presenting this view controller modally to blur the underlying content, you'll need to set the modal presentation style to Over Current Context and set the background color to clear to ensure the underlying view controller will remain visible once this is presented overtop.

这篇关于创建一个模糊重叠视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆