1

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?

Ninja
  • 157
  • 5
  • 22

1 Answers1

2

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.

Rob
  • 371,891
  • 67
  • 713
  • 902
  • Thank you very much @Rob ! Actually i was puzzled with this line: " yet the animation works because the animation server retains it." Well we instantiate as a local variable animation class inside some function, i think there is already is a strong reference to animation class, why he is using word YET? ARC is not 0 in this case so why do we need the retaining of animation server? – Ninja Aug 05 '20 at 04:25
  • His whole point is that when you start the animation, the “animation server” keeps its own strong reference to it so you don't have to. His use of the word “yet” is to say “hey, you might think you need to keep your own strong reference to keep this from being deallocated, but you don't.” – Rob Aug 05 '20 at 04:27
  • He writes about it as if earlier people were needed to create reference in that aforementioned case – Ninja Aug 05 '20 at 04:33