Move CALayer via UIBezierPath

好久不见. 提交于 2019-12-04 07:32:56

Hope this will be helpful.

UIBezierPath *trackPath = [UIBezierPath bezierPath];
.
.
.
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
anim.path = trackPath.CGPath;
anim.repeatCount = 1;
anim.duration = 2.0;
[layerToAnimate addAnimation:anim forKey:@"pathGuide"];

If your bezier path is a circle or even a half circle, you could just skip the path and instead add the point to a larger square layer. Set the point to be a fixed distance from the center of that larger square layer. Then you can just rotate the larger layer on the z axis around the center whatever number of degrees you need it to move. Seems it would simplify things a bit, though I'm not sure exactly what else you need it to do.

i'd been looking to do something like this and fount this tutorial, it shows how to follow a specific path. It works with a car (a CALayer) and a UIBezierpath as the race track and is solved in this order:

Defining the path the car should follow (in this case your BezierPath) Drawing the black line that defines the track; (N/A) Drawing the white dashed center-line of the track; (N/A) Creating the layer defining the car; (your Layer) Animating the car along the path. (What your asked!)

You can check the reference Post here: http://nachbaur.com/2011/01/07/core-animation-part-4/

Also you can download the source code here: http://cdn5.nachbaur.com/wp-content/uploads/2011/01/CALayerAnimTest.zip?25881d

hope this helps!

regards,

Jorge.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!