Move CALayer via UIBezierPath

99封情书 提交于 2019-12-21 13:58:11

问题


I have a layer that will move from point A to point B on a UIBezierPath. I have found a lot of samples those are refers to CAAnimation and UIBezierPath. But I need to move my layer only from specified point to another on bezier path.

Any suggestions would be appreciated.

Thanks


回答1:


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"];



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/13379100/move-calayer-via-uibezierpath

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