I have an image, I animate it with this code, in viewDidAppear:
UIView.animateWithDuration(10.5, delay:0.0, options: [], animations:{
self.myImage.transform = CGAffineTransformMakeTranslation(0.0, 200)
}, completion: nil)
I want to pause the animation when I tap myPauseButton
, and resume the animation if I tap it again.
Since iOS 10 provides
UIViewPropertyAnimator
you can solve your problem easier.Create these properties in your controller:
Add the following code to the tap handler of
myPauseButton
:Start animation from
viewDidAppear(_ animated: Bool)
with these lines of code:2 functions to pause and resume animation, I take from here and convert to Swift.
I have a button to pause or resume the animation which is initialed in
viewDidLoad
:Here is Swift 3 version of that answer + I moved those function to an extension