I am having an issue with UIViewPropertyAnimator, setup as follows:
let animator = UIViewPropertyAnimator(duration: 6.0, curve: .linear)
animator.addAnimations {
UIView.animateKeyframes(withDuration: 6.0, delay: 0.0) {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.1) {
someView.alpha = 1.0
}
UIView.addKeyframe(withRelativeStartTime: 0.9, relativeDuration: 0.1) {
someView.alpha = 0.0
}
}
}
@objc func didTapButton {
if animator.isRunning {
animator.isReversed = !animator.isReversed
} else {
print("start")
animator.startAnimation()
}
}
The first time I hit the button the animation plays fine. However the second time I hit it (after the animation completes) nothing happens. The animator has definitely stopped running (checked via the print statement) but it just doesn't respond.
What am I doing wrong here?
According to
UIViewPropertyAnimatordocumentation:In other words, when you call didTapButton second time, your animator has no animations.
To fix it, you should
addAnimationsevery time user taps the button.