我想在按下按鈕時查看頁面。我希望按鈕模糊背景并覆寫文本,而不僅僅是模糊的背景。我在顯示文本時無法顯示模糊的背景。請看我下面的代碼...
問題出在哪里->>
Button {
withAnimation {
show.toggle()
}
} label: {
VStack {
ZStack {
Rectangle()
.fill(Color.white.opacity(0.5))
.frame(maxWidth: 170, maxHeight: 60)
.cornerRadius(15)
.padding([.leading, .trailing])
Image(systemName: "pause")
.font(.title)
.foregroundColor(.black)
}
}
}
}
.padding(.trailing, 6)
.padding(.bottom)
Spacer(minLength: 65)
}
}
}.blur(radius: show ? 100 : 0)
uj5u.com熱心網友回復:

您可以使用這種方法來實作您正在尋找的東西
import SwiftUI
struct BlurBackground: View {
// MARK: - PROPERTIES
@State private var showNextView: Bool = false
// MARK: - BODY
var body: some View {
ZStack{
Color.orange
.edgesIgnoringSafeArea(.all)
.blur(radius: showNextView ? 8.0 : 0.0) //Blur
VStack(spacing: 40){
Text("First View")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(.white)
Image(systemName: "apps.iphone")
.resizable()
.scaledToFit()
.frame(height: 200)
.foregroundColor(.white)
Button("Show Next View") {
showNextView = true
}
.padding(.horizontal, 30)
.padding(.vertical, 15)
.background(.white)
}//: VSTACK
.blur(radius: showNextView ? 8.0 : 0.0) //Blur
if showNextView{
VStack {
Text("Second View")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(.white)
Image(systemName: "laptopcomputer")
.resizable()
.scaledToFit()
.frame(height: 200)
.foregroundColor(.white)
}//: VSTACK
}
}//: ZSTACK
}
}
// MARK: - PREVIEW
struct BlurBackground_Previews: PreviewProvider {
static var previews: some View {
BlurBackground()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430552.html
