I have a view that starts animating when a ViewController first opens. It works fine on initial launch, but when returning to the view after leaving the animation does not restart. The views are embedded in a Navigation controller
I tried placing the animation in several locations, including viewWillAppear, viewDidAppear and viewWillLayoutSubviews
I've placed a breakpoint in viewDidAppear and it does step through the code, but the animation does not restart.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
UIView.animate(withDuration: 20.0, delay: 0, options: [.autoreverse, .repeat, .curveLinear], animations: {
self.scrollingView.transform = CGAffineTransform(translationX: -350, y: 0)
}, completion: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
scrollingView.layer.removeAllAnimations()
}
You need to return the scrollingView
to its original position during the segue
, because of the UINavigationController
the 1st UIViewController
does not go out of scope, aka it does not get instantiated again. Therefore when you come back to the 1st UIViewController
, the scrollingView
is still running the previous animation.
You need to restart the CGAffineTransform using the code below :
override func viewDidDisappear(_ animated: Bool) {
self.textView.transform = CGAffineTransform.identity
}