Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After quite a lot of searching and talks with iOS development gurus, it appears that <a href="http://developer.apple.com/library/ios/#qa/qa1673/_index.html#//apple_ref/doc/uid/DTS40010053" rel="noreferrer">QA1673</a> doesn't help when it comes to pausing, backgrounding, then moving to foreground. My experimentation even shows that delegate methods that fire off from animations, such as<code>animationDidStop</code>become unreliable.</p> <p>Sometimes they fire, sometimes they don't. </p> <p>This creates a lot of problems because it means that, not only are you looking at a different screen that you were when you paused, but also the sequence of events currently in motion can be disrupted.</p> <p>My solution thus far has been as follows:</p> <p>When the animation starts, I get the start time:</p> <p><code>mStartTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];</code></p> <p>When the user hits the pause button, I remove the animation from the<code>CALayer</code>:</p> <p><code>[layer removeAnimationForKey:key];</code></p> <p>I get the absolute time using<code>CACurrentMediaTime()</code>: </p> <p><code>CFTimeInterval stopTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];</code></p> <p>Using the<code>mStartTime</code>and<code>stopTime</code>I calculate an offset time:</p> <pre><code>mTimeOffset = stopTime - mStartTime; </code></pre> <p>I also set the model values of the object to be that of the<code>presentationLayer</code>. So, my<code>stop</code>method looks like this:</p> <pre><code>//-------------------------------------------------------------------------------------------------- - (void)stop { const CALayer *presentationLayer = layer.presentationLayer; layer.bounds = presentationLayer.bounds; layer.opacity = presentationLayer.opacity; layer.contentsRect = presentationLayer.contentsRect; layer.position = presentationLayer.position; [layer removeAnimationForKey:key]; CFTimeInterval stopTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; mTimeOffset = stopTime - mStartTime; } </code></pre> <p>On resume, I recalculate what's left of the paused animation based upon the<code>mTimeOffset</code>. That's a bit messy because I'm using<code>CAKeyframeAnimation</code>. I figure out what keyframes are outstanding based on the<code>mTimeOffset</code>. Also, I take into account that the pause may have occurred mid frame, e.g. halfway between<code>f1</code>and<code>f2</code>. That time is deducted from the time of that keyframe.</p> <p>I then add this animation to the layer afresh:</p> <p><code>[layer addAnimation:animationGroup forKey:key];</code></p> <p>The other thing to remember is that you will need to check the flag in<code>animationDidStop</code>and only remove the animated layer from the parent with<code>removeFromSuperlayer</code>if the flag is<code>YES</code>. That means that the layer is still visible during the pause.</p> <p>This method does seem very laborious. It does work though! I'd love to be able to simply do this using <a href="http://developer.apple.com/library/ios/#qa/qa1673/_index.html#//apple_ref/doc/uid/DTS40010053" rel="noreferrer">QA1673</a>. But at the moment for backgrounding, it doesn't work and this seems to be the only solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload