swiftui_SwiftUI中的模糊效果

swiftui

If you’ve been around smart phones long enough, you may remeber iOS 7. It was a huge update to the iPhone, bringing in not only a tone of new features and functionality, but also a fresh new design. This update to iOS was nothing less than a full-fledged overhaul of the operating system’s look and feel.

如果您使用智能手机的时间已经足够长了,您可能会考虑使用iOS7。这是对iPhone的巨大更新,不仅带来了新功能和新功能,而且还带来了全新的设计。 对iOS的此次更新无非是对操作系统外观的全面革新。

One of the more interesting changes was the presence of semi-translucent toolbars and backgrounds like Control Center. That effect of “blurred” sort of “frosted-glass” background which is just transparent enough to be able to tell that there is something behind it. It’s so nice to look at

最有趣的更改之一是半透明的工具栏和诸如Control Center的背景。 这种“模糊的”“磨砂玻璃”背景的效果是足够透明的,足以分辨出背后有东西。 看真好

These days there is a very straightforward way to achieve the blur effect by means of UIVisualEffectView.

UIVisualEffectView有一种非常简单的方法可以通过UIVisualEffectView实现模糊效果。

You can tell right away that this comes from UIKit which means there are probably a few tweaks needed in order to use it in SwiftUI.

您可以马上说出它来自UIKit ,这意味着可能需要一些调整才能在SwiftUI中使用它。

Let’s get started!

让我们开始吧!

创建BlurView (Creating the BlurView)

When we need UIKit components in SwiftUI, we normally turn for help to the UIViewRepresentable protocol. The purpose of this protocol is for us to sort of translate the SwiftUI state and lifecycle events to UIKit. For example, in SwiftUI views are often destroyed and recreated when their state changes while in UIKit a UIView is usually kept around and updated.

当我们在SwiftUI中需要UIKit组件时,通常我们会向UIViewRepresentable协议寻求帮助。 该协议的目的是让我们将SwiftUI状态和生命周期事件转换为UIKit 。 例如,在SwiftUI中,视图的状态更改时通常会销毁它们并重新创建视图,而在UIKit中,通常会保留并更新UIView。

Let’s begin by creating a BlurView which implements the UIViewRepresentable protocol.

首先,让我们创建一个BlurView它实现了UIViewRepresentable协议。

设置UIViewRepresentable (Setting up UIViewRepresentable)

The two important methods here are makeUIView which will be called when the view needs to be initialised and updateUIView which is called whenever SwiftUI detects changes and asks the BlurView to update its layout, position etc.

这里的两个重要方法是makeUIView (在需要初始化视图时调用)和updateUIView (在SwiftUI检测到更改并要求BlurView更新其布局,位置等时调用)。

Here, we are also going to specify the type of UIKit view which we are going to use, a UIVisualEffectView

在这里,我们还将指定将要使用的UIKit视图的类型,即UIVisualEffectView

创建UIVisualEffectView (Creating UIVisualEffectView)

The UIVisualEffectView is an object which helps us create visual effects. In our case, we are aiming to create a blur effect which is represented by UIBlurEffect:

UIVisualEffectView是一个对象,可以帮助我们创建视觉效果。 在我们的例子中,我们的目标是创建一个由UIBlurEffect表示的模糊效果:

UIVisualEffectView(effect: UIBlurEffect(style: .systemMaterial))

Blur effects come in several flavours, as described by the UIBlurEffect.Style enumeration. In the snipped above, I am using systemMaterial as a default.

UIBlurEffect.Style枚举所述,模糊效果有几种风格。 在上面的片段中,我使用systemMaterial作为默认值。

Now theBlurView looks like this:

现在, BlurView看起来像这样:

You may be asking why do we need to re-apply the blur effect when the view updates? The answer lies within UIKit, the blur effect takes into account the entire view hierarchy. For instance, as the blur effect creates the sensation of transparency, whenever the content under our BlurView changes, the blur will need to be re-applied.

您可能会问,为什么视图更新时我们需要重新应用模糊效果? 答案在于UIKit,模糊效果考虑了整个视图层次。 例如,由于模糊效果产生了透明感,每当BlurView下的内容更改时,都需要重新应用模糊。

As of now, the blur style is hardcoded to .systemMaterial. It will be nice if we could make it possible to choose the blur effect style when BlurView is used in a layout.

到目前为止,模糊样式已硬编码为.systemMaterial 。 如果在布局中使用BlurView时可以选择模糊效果样式,那将是很好的。

For this, I will create an initialiser which receives the specific style to be used as an argument:

为此,我将创建一个初始化程序,该初始化程序接收要用作参数的特定样式:

让我们测试一下! (Let’s test it!)

That’s a big blob of code but let’s see how it looks.

那是一大堆代码,但让我们看一下它的外观。

The BlurView which we created can now be used as a background view to different components. Something like this:

我们创建的BlurView现在可以用作不同组件的背景视图。 像这样:

VStack(spacing: 6) {
Text("BlurViews are awesome")
}
.background(BlurView())

A blur effect on a plain white background will not be very interesting, so I am going to fill the view below with a solid colour so we can witness the effect.

在纯白色背景上的模糊效果不会很有趣,因此我将在下面的视图中填充纯色,以便我们可以看到效果。

Preview of the ContentView.swift ContentView.swift的预览
  • I am using a ZStack to order the content of the view — background colour, and then on top of it a VStack containing some text.

    我正在使用ZStack来排序视图的内容(背景色),然后在其之上是包含一些文本的VStack。
  • The BlurView is applied as a background of the VStack.

    BlurView被用作VStack的背景。

It looks even better if we use a picture as a background instead of a solid color:

如果我们将图片用作背景而不是纯色,则看起来会更好:

翻译自: https://medium.com/dev-genius/blur-effect-with-vibrancy-in-swiftui-bada837fdf50

swiftui

你可能感兴趣的:(python)