Set Start Point for CGAffineTransform
-
22-10-2019 - |
Question
I am animating a UIView along a circle using a CAKeyframeAnimation that follows a CGPathAddEllipseInRect. However, the view always seems to start in the same place regardless of the frame it is originally positioned in. Is there some way to adjust the starting position of the view on the path?
Thanks!
CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;
CGMutablePathRef animationPath = CGPathCreateMutable();
CGPathAddEllipseInRect(animationPath, NULL, rect);
myAnimation.path = animationPath;
CGPathRelease(animationPath);
[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];
Solution
I don't think it's possible to set a start point for CGPathAddEllipseInRect. (At least i wasn't successful)
instead of using: CGPathAddEllipseInRect, You should use: CGPathAddArc! For example:
CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;
CGMutablePathRef animationPath = CGPathCreateMutable();
NSInteger startVal = M_PI / 2;
//seventh value (end point) must be always 2*M_PI bigger for a full circle rotation
CCGPathAddArc(animationPath, NULL, 100, 100, 100, startVal, startVal + 2*M_PI, NO);
myAnimation.path = animationPath;
CGPathRelease(animationPath);
[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];
will rotate full circle starting from bottom - clockwise. Change startVal value to change rotation start position. (full rotation is 2*M_PI).
OTHER TIPS
Previous answer is correct and works, but there should be
CGFloat startVal = M_PI / 2;
instead of
NSInteger startVal = M_PI / 2;
Otherwise, M_PI will be rounded to integer and you won't be able to achieve accurate angle. P.S. has low reputation to comment the previous answer, sorry.