Animation not working when disappearing, but works when appearing

449 views Asked by At

I'm trying to make a button appear and disappear (animated). When appearing, it animates. But when the button is supposed to disappear it doesn't animate, it just goes away.

anybody any idea why when it appears it animates, but when it disappears it doesn't?

Then button appears when a switch is turned on and disappears when the switch is turned off again.

if switchesEnabled > 0 {
     UIView.transition(with: button, duration: 0.25, options: .transitionCrossDissolve, animations: {self.button.isHidden = true})
} else {
     UIView.transition(with: button, duration: 0.25, options: .transitionCrossDissolve, animations: {self.button.isHidden = false})
}

additionally: the button also disappears when the button itself is pressed and, using exactly the same line of code, in that case it works.

1

There are 1 answers

0
aheze On BEST ANSWER

Instead of using UIView.transition you should use UIView.animate.

if switchesEnabled > 0 {
    UIView.animate(withDuration: 0.25, animations: {
        self.button.alpha = 0
    })
} else {
    UIView.animate(withDuration: 0.25, animations: {
        self.button.alpha = 1
    })
}

Setting the alpha to 0 will automatically disable the button.