iPhone - 如何为CAKeyframeAnimation创建圆形路径?

时间:2010-09-27 13:51:21

标签: iphone animation cakeyframeanimation

我正在尝试制作关键帧动画,让图像视图围绕圆圈飞行。我正在使用CGPathAddCurveToPoint来制作关键帧点。问题是图像视图总是使用最短(线性)路径。

我想有一些为给定半径构建圆路径的函数。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
CGPathAddQuadCurveToPoint(path, NULL,middlePoint.x, middlePoint.y,endPoint.x,endPoint.y);

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.removedOnCompletion = NO;
pathAnimation.path = path;
[pathAnimation setCalculationMode:kCAAnimationCubic];
[pathAnimation setFillMode:kCAFillModeForwards];
pathAnimation.duration = 2.0;

[viewTobemoved.layer addAnimation:pathAnimation forKey:nil];

此动画仅使用一个贝塞尔点,并从起点到终点形成一个软圆。如果您想通过定义圆的半径来精确定义曲线,可以参考以下链接

http://blog.shoguniphicus.com/2011/05/19/keyframe-animation-ios-cakeyframeanimation-objective-c/

答案 1 :(得分:2)

这可以解决这个问题吗?

  CAKeyframeAnimation* circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"center"];

  CGMutablePathRef circularPath = CGPathCreateMutable();
  CGRect pathRect = ...; // some rect you define. A circle has equal width/height; radius is half the values you specify here
  CGPathAddEllipseInRect(circularPath, NULL, pathRect);
  spinAnimation.path = circularPath;
  CGPathRelease(circularPath);

您会注意到动画的关键路径是您想要围成一圈飞行的UIView的中心属性。