Since iOS 10 provides UIViewPropertyAnimator
you can solve your problem easier.
Declare these properties in your controller:
var animationPaused = false
lazy var animator: UIViewPropertyAnimator = UIViewPropertyAnimator(duration: 10.5, curve: .easeInOut, animations: {
self.myImage.transform = CGAffineTransform(translationX: 0.0, y: 200)
})
Add the following code to the tap handler of myPauseButton
:
if self.animator.state == .active { // Don't start or pause the animation when it's finished
self.animationPaused = !self.animationPaused
self.animationPaused ? self.animator.pauseAnimation() : self.animator.startAnimation()
}
Start the animation in viewDidAppear(_ animated: Bool)
with these lines of code:
self.animationPaused = false
self.animator.startAnimation()