简体   繁体   中英

BezierPath animation

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM