swiftui:如何在满足条件后禁用按钮并对其进行模糊处理?

swiftui: how can i disable a button and blur it after a condition is met?

我在 swiftUi 中有一个按钮。当用户按下该按钮时,计数器从 5 开始减少。一旦计数器归零,该按钮将被禁用。到目前为止,我已经让代码正常工作了:

Button(action: {
    tnum = tnum - 1
}) {
    Text("Press Me!")
       .background(Color.purple) 
       .foregroundColor(.white)  
       .font(.title)             
       .padding() 
}
.disable(tnum <= 0)

我知道如何模糊按钮。它会类似于 .blur(radius: 3, opaque: false) 但我只希望在数字为零时执行此操作。

我怎样才能做到这一点?

如果半径为零,则模糊无效,因此您可以使用

Button(action: {
    tnum = tnum - 1
}) {
    Text("Press Me!")
       .background(Color.purple) 
       .foregroundColor(.white)  
       .font(.title)             
       .padding() 
}
.blur(radius: tnum <= 0 ? 0 : 3, opaque: false)  // << here !!
.disable(tnum <= 0)