Currently i am reading very interesting book of Matt Neuburg. But i stuck on this moment:
In the simplest case, you’ll just launch the animation and stand back, as I demonstrated earlier:
let anim = UIViewPropertyAnimator(duration: 1, curve: .linear) { self.v.backgroundColor = .red } anim.startAnimation()
In that code, the UIViewPropertyAnimator object anim is instantiated as a local variable, and we are not retaining it in a persistent property; yet the animation works because the animation server retains it.
Did we ever need to retaining an animation in a persistent property when we using it in aforementioned way? Why animation (it seems in other cases) should not work if we do not retain it in a persistent property? I think i don not understand something who else read this book?
Matt’s point is that you don’t need to keep a reference to it for the animation to complete. He’s not saying that you can’t keep a reference, only that you don’t have to.
You ask:
Did we ever need to retaining an animation in a persistent property when we using it in [aforementioned] way?
No, you don’t need to “retain” it for it to continue animating.
You might ask why you might keep a reference: You might do that if your want to pause it or stop it, scrub it, or whatever, after the animation has already started.
Bottom line, if you need a reference for other reasons, fine, keep a reference to it. Otherwise, making it a local variable and starting it is all you need to do.
Why animation (it seems in other cases) should not work if we do not retain it in a persistent property?
That’s not what he’s saying. He’s saying the precise opposite, namely that you don’t have to keep a strong reference to it for the animation to continue. Keep a reference if you need it for other reasons, but not simply to ensure that the animation continues.