[英]Create Background-blurred RoundedRectangle in SwiftUI
我正在使用此代碼創建背景模糊視圖:
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)
}
}
但是,此代碼返回一個背景模糊的常規矩形。 我如何使它變圓?
您可以創建另一個 SwiftUI 視圖:
struct BlurCircle: View {
var body: some View {
Blur()
.clipShape(Circle())
}
}
並像這樣使用它:
struct ContentView: View {
var body: some View {
BlurCircle()
.frame(width: 100, height: 100)
}
}
或者,您可以這樣做:
struct ContentView: View {
var body: some View {
Blur()
.frame(width: 100, height: 100)
.clipShape(Circle())
}
}
如果您不想在Blur
結構之外執行此操作,則可以查看以下答案:
您需要更改視覺效果視圖的兩個屬性cornerRadius
和clipsToBounds
。 你的makeUIView
function 看起來像
func makeUIView(context: Context) -> UIVisualEffectView {[]
let view = UIVisualEffectView(effect: UIBlurEffect(style: style))
view.cornerRadius = 20 // change this to your radius
view.clipsToBounds = true
return view
}
' struct BlurRectangle: View { var body: some View { RoundRectangle(cornerRadius: 25).background(Blur()).frame(width: 100, height: 100) } } '希望對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.