有没有一种方法可以在 SwiftUI 中模糊背景? [英] Is there a method to blur a background in SwiftUI?
问题描述
我想模糊视图的背景,但又不想闯入 UIKit 来完成它(例如,一个 UIVisualEffectView
)我正在挖掘文档却一无所获,似乎没有办法实时剪辑背景并对其应用效果.我错了还是以错误的方式看待它?
I'm looking to blur a view's background but don't want to have to break out into UIKit to accomplish it (eg. a UIVisualEffectView
) I'm digging through docs and got nowhere, seemingly there is no way to live-clip a background and apply effects to it. Am I wrong or looking into it the wrong way?
推荐答案
1.Native SwiftUI 方式:
只需在需要模糊的任何内容上添加 .blur()
修饰符,例如:
Image("BG")
.blur(radius: 20)
注意视图的顶部和底部
请注意您可以Group
多个视图并将它们模糊在一起.
Note that you can Group
multiple views and blur them together.
你可以从 UIKit 中引入完美的 UIVisualEffectView
:
You can bring the prefect UIVisualEffectView
from the UIKit:
VisualEffectView(effect: UIBlurEffect(style: .dark))
使用这个小结构:
struct VisualEffectView: UIViewRepresentable {
var effect: UIVisualEffect?
func makeUIView(context: UIViewRepresentableContext<Self>) -> UIVisualEffectView { UIVisualEffectView() }
func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext<Self>) { uiView.effect = effect }
}
您可以使用一行代码使用 iOS 预定义材料:
You can use iOS predefined materials with one line code:
.background(.ultraThinMaterial)
这篇关于有没有一种方法可以在 SwiftUI 中模糊背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!