2010-09-27 64 views
3

我试图制作一个关键帧动画,让图像视图围绕一个圆圈飞行。我使用CGPathAddCurveToPoint来制作关键帧点。问题是图像视图总是使用最短(线性)路线。iPhone - 如何为CAKeyframeAnimation制作圆形路径?

我想有某种函数为给定半径建立一个圆弧路径。

非常感谢您的帮助。

回答

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的中心属性。

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/

+0

什么是'startPoint.x'和'startPoint.y',用'middlePoint.x&.y'和'endPoint.x&沿着y'? – topLayoutGuide 2013-01-08 04:32:46

+0

起始点决定曲线开始的位置,曲线在中间点上形成一个软圆。您需要为目标c中的CGPoint定义x和y坐标 – 2013-01-08 07:50:58