swift - SwiftUI 中有没有一种模糊背景的方法?
问题描述
我正在寻找模糊视图的背景,但不想闯入 UIKit 来完成它(例如 a UIVisualEffectView
)我正在挖掘文档但一无所获,似乎没有办法实时剪辑背景并对其应用效果。我错了还是以错误的方式调查它?
解决方案
1. Native SwiftUI 方式:
.blur()
只需在您需要模糊的任何内容上添加修饰符,例如:
Image("BG")
.blur(radius: 20)
请注意,您可以使用Group
多个视图并将它们模糊在一起。
2.视觉效果视图:
你可以UIVisualEffectView
从 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 }
}
3. iOS 15:材质
您可以通过一行代码使用 iOS 预定义材料:
.background(.ultraThinMaterial)
推荐阅读
- android - 颤振返回“未来的实例
" 而不是期望值 - html - 为什么文本内容会使列变宽?
- r - 如何在 R 中指定文本分割?
- javascript - 为什么不使用“new”会影响 Javascript 的性能
- r - 删除重复自身的某个键左侧的所有文本
- visual-studio-code - 如何获取用户从 VSCode 扩展打开的 ViewColumns 的数量?
- java - 将 parseUnsignedInt 移植到 java1.7
- c++ - 是否可以定义一个队列列表,其中每个队列可以是不同的类型?
- flutter - 带有 ListView 的应用程序不会启动。显示没有错误
- docker - okd(openshift) pod 未将结果写入磁盘