Infinite CABasicAnimation stops
Actually, there is a simple way to solve this
setting your animation to this:
Swift 3 version
animation.removedOnCompletion = false
Swift 4 version
animation.isRemovedOnCompletion = false
The layer itself isn't destroyed, when NavigationController is being pushed to another ViewController's view, because UINavigationController got viewControllers
property which will retain the original viewController and therefore its view and your animated layer.
It is this CABasicAnimation object destroyed when view removed from interface even though you set its repeatCount to infinite. So set the isRemovedOnCompletion
to false to keep it.
When a layer is removed from the screen, it loses its animations. You need to add them back once the layer is on the screen again.
viewWillAppear:
is too early, because the layer isn't actually on the screen by that time. Try adding the animation in viewDidAppear:
.