ios - Create Background-blurred RoundedRectangle in SwiftUI
问题描述
I am using this code to create a background-blurred view:
struct Blur: UIViewRepresentable {
let style: UIBlurEffect.Style = .systemUltraThinMaterial
func makeUIView(context: Context) -> UIVisualEffectView {
return UIVisualEffectView(effect: UIBlurEffect(style: style))
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
uiView.effect = UIBlurEffect(style: style)
}
}
However, this code returns a regular rectangle with background blur. How do I make it rounded?
解决方案
You can create another SwiftUI view:
struct BlurCircle: View {
var body: some View {
Blur()
.clipShape(Circle())
}
}
and use it like this:
struct ContentView: View {
var body: some View {
BlurCircle()
.frame(width: 100, height: 100)
}
}
Alternatively, you can just do:
struct ContentView: View {
var body: some View {
Blur()
.frame(width: 100, height: 100)
.clipShape(Circle())
}
}
If you don't want to do this outside the Blur
struct, you can take a look at this answer:
推荐阅读
- excel - 在excel中查找和替换
- java - Spring Boot:从属性文件中读取值列表
- python - 如何使用python递归方法解决sigma求和
- c++ - 将 std::function 作为模板参数传递给另一个函数不会编译
- angular - 以角度向当前 utc 日期添加天数
- powershell - PowerShell“无法将“Object[]”类型的“System.Object[]”值转换为“array”类型”
- c# - 将 VS 2017 连接到 Oracle 数据库
- ios - 如何为每个 TableViewCell 设置不同的颜色?
- linux - 将一行添加到 txt 文件开头的最佳/最快方法
- php - WooCommerce 订阅 3.0 | 手动运行计划的操作不起作用