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

Time:02-06

I have a button in swiftUi. When the user presses that button the counter goes down from 5. Once the counter hits zero, the button will become disabled. I have got the code working till this point:

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

I know how to blur a button. It would be something like .blur(radius: 3, opaque: false) but I only want this to be executed if the number is zero.

How can I achieve this?

CodePudding user response:

Blur has no effect if radius is zero, so you can use

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)
  •  Tags:  
  • Related