Technical Q&A QA1673

How to pause the animation of a layer tree

Q:  How do I pause all animations in a layer tree?

A: In order to pause animations in a layer tree, you can take advantage of the fact that a CALayer conforms to the CAMediaTiming protocol. The CAMediaTiming protocol defines, among other things, a speed with which its timeline progresses, which you can use to pause all animations on the target layer. Listing 1 demonstrates how you can do this.

Listing 1  Pause and Resume animations.

    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;

    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;

Document Revision History


Fixed typo in code listing.


New document that describes how you can pause animations in a layer tree