tomknig / TOMSMorphingLabel

Configurable morphing transitions between text values of a label.
MIT License
1.86k stars 104 forks source link

Break the strong reference cycle between CADisplayLink and TOMSMorphingLabel. #22

Closed wanderwaltz closed 9 years ago

wanderwaltz commented 9 years ago

Hi, I've implemented a simple solution to the retain cycle problem described in #12. Please tell if that works for you.

CADisplayLink retains the object set as its target and TOMSMorphingLabel references the display link strongly. This gives a strong reference cycle and TOMSMorphingLabel is never deallocated. This PR adds a helper class named TOMSMorphingLabelWeakWrapper which holds a weak reference to the label and forwards necessary messages to it. This breaks the reference cycle and allows deallocating the label.

Note that if the weak wrapper's label reference gets nullified due to the label being deallocated, the display link would continue firing its events to the weak wrapper. So it is necessary to properly invalidate the display link in the TOMSMorphingLabel's dealloc.

tomknig commented 9 years ago

Hi Egor,

very clever idea to break the cycle. Thank you very much for you pull request. I have merged it and it is available in version 0.5.

Thanks, Tom

wanderwaltz commented 9 years ago

Glad I could help. :+1: