1

I have a Bezier curve and im animating an object to move along it using CAKeyFrameAnimation. But, say the object stops at some point in the curve on detection of a user tap. Then on clicking a button to resume the movement, the object should move along the remaining path to the end. How do i do this?

If i add the path to the CAKeyFrameAnimation object, then on clicking the resume button, the object moves right from the starting to the end of the path. Not from the point it stopped. I want it to continue to move only from the point it stopped and not start from the beginning again. Any way to animate it along the path from the point it stops at?

1 Answer 1

3

All of the Core Animation timing is implemented in the CAMediaTiming protocol. One of its properties is speed which defines the animation timings for a layer relative to the time space of its parent layer. Setting the speed property to 0.0 will effectively pause the animation.

You have to do a little time housekeeping to make sure that the animation finishes properly once you restart it. Technical Q&A QA1673 in the docs has the following example code which should work for you:

-(void)pauseLayer:(CALayer*)layer
{
  CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
  layer.speed = 0.0;
  layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CAlayer*)layer
{
  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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.