当另一个视图处于活动状态时,如何模糊背景



当第二个视图处于活动状态时,我尝试模糊第一个VStack。但是当这样做时;启用模糊";每次点击按钮时,其背景颜色都会发生变化。我不确定我在哪里出了问题,我想知道是否有更好的方法。

struct ContentView: View {

@State private var showWindow = false
var body: some View {
ZStack{
VStack{
Button(action: {self.showWindow.toggle()}){
Text("Enable Blur")
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(5)
}
}.blur(radius: showWindow ? 5 : 0)
VStack{
if(showWindow){
DatePickerView(showWindow: self.$showWindow)
}
}
}
}
}
struct DatePickerView: View {
@Binding var showWindow: Bool
var body: some View{
VStack{
Button(action: {self.showWindow.toggle()}){
Text("Done").foregroundColor(.white)
}
}
}
}

模糊效果会累积,因为VStack内容未被SwiftUI渲染引擎确定为已更改,因此其缓存的渲染变体将用于下一次重绘。。等等

要修复此问题,我们需要将VStack标记为刷新。以下是可能的解决方案。使用Xcode 11.4/iOS 13.4 进行测试

VStack{
Button(action: {self.showWindow.toggle()}){
Text("Enable Blur")
.foregroundColor(.white)
.padding()
.background(Color.black)
.cornerRadius(5)
}
}.id(showWindow)                  // << here !!
.blur(radius: showWindow ? 5 : 0)

最新更新