Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move CALayer via UIBezierPath

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.

enter image description here

Any suggestions would be appreciated.

Thanks

like image 707
fyasar Avatar asked Nov 14 '12 12:11

fyasar


3 Answers

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"];
like image 81
Mrug Avatar answered Oct 05 '22 22:10

Mrug


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.

like image 38
Matt Long Avatar answered Oct 06 '22 00:10

Matt Long


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.

like image 32
Jorge Vicente Mendoza Avatar answered Oct 05 '22 22:10

Jorge Vicente Mendoza