Search code examples
iosanimationrotationcore-animationcalayer

Pause and resume animations except of rotation in iOS?


If I need simply to pause/resume rotation then I use the following code:

- (void)pauseAnimations
{
    CFTimeInterval paused_time = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.layer.speed = 0.0;
    self.layer.timeOffset = paused_time;
}

- (void)resumeAnimations
{
    CFTimeInterval paused_time = [self.layer timeOffset];
    self.layer.speed = 1.0f;
    self.layer.timeOffset = 0.0f;
    self.layer.beginTime = 0.0f;
    CFTimeInterval time_since_pause = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil] - paused_time;
    self.layer.beginTime = time_since_pause;
}

But I want to stop the current layer animation before rotation and resume after it. And this code stops the rotation process too.

How to solve this issue?


Solution

  • You can't pause a layer's animation's all but one by setting the layer's speed to 0.

    You might be able to fetch the active animations from the layer and pause the individual animations using the technique you are using to pause animations on the whole layer (setting the speed to 0).

    Note that you can't save a pointer to the animation objects that you create because the system actually adds a copy of the animation to the layer, not the original animation. You would probably need to fetch the animation using the CALayer animationForKey method.

    Disclaimer: I have never tried this. My answer is based on general knowledge of how Core Animation works.