Search code examples
ioscabasicanimation

Do animations use juice while the view is hidden?


I have this small view V

v: UICrazyView

which has sundry animations, which run often, and which follow various annoying states and inputs.

Now on top of that, from time to time the whole thing is just hidden

var slideAwaySomePanel: Bool {
  didSet {
    .. do many other things
    v.isHidden = slideAwaySomePanel
  }
}

It might be hidden for a minute, an hour, forever, or never.

It occurred to me, while V is hidden ... are the animations still running?

Do they still use a lot of battery/performance?

I was about to override isHidden and start writing a whole lot of fragile PITA code that would know what to do as isHidden is toggled, but maybe that is pointless.

I wish to know

  1. When you isHidden, do all the calculations (and even drawing?) continue for the ongoing animations? Are you still using battery? Should we carefully stop everything during isHidden to save battery / performance. Or, does isHidden stop everything anyway? Or does it still do the timers and curves, but not waste any power on drawing?

  2. Do all the timers and so on actually "pause" when you go in to isHidden? If you have an endless repeating animation, or, a 10 second fade or such, does it "hold" at an exact position, and continue when you not isHidden? Or what happens?

In short should we carefully and tediously stop by-hand animations, when isHidden is in effect? Or is there no point?

(It occurs to me, this is very much like in cg where you either do or don't have animations or other physics ongoing when objects are occluded or out of the frustrum. For this reason game engines simply have a toggle to decide on exactly that behavior "keep going or not when I'm offscreen?")


Solution

  • I'm pretty sure, even though I have no reference, that hidden views are not animated because Core Animation was implemented very efficiently in terms of performance.

    Core animation layers and animations have their own clock. The animation state is calculated from this time. The clock continues to run when the view is not visible. Since neither the layer nor the animation object are destroyed by hiding the view, the animation has exactly the same state after reappearing that it would have had if the view had not been hidden.

    Apple gives some nice examples how to modify the animation timing for some use cases.