CGPath动画

3 core-animation

我需要一些示例代码,我可以在其中设置曲线/弧形路径的动画,该路径应绘制一个完整的圆形动画?

任何输入将不胜感激.

谢谢,周六

Mat*_*ong 10

更新:意识到有一个更好的解决方案来解决这个问题.只需创建一个圆形路径,并将CAShapeLayer的strokeEnd属性设置为0.0f到1.0f.看看Ole的回答:在iPhone上用CAKeyFrameAnimation绘制路径

原始答案:

您可以使用CAKeyframeAnimation并创建Core Graphics路径.此代码以抛物线模式激活图层,但您可以对其进行调整以绘制圆形.

- (CAAnimation*)pathAnimation;
{

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path,NULL,50.0,120.0);

    CGPathAddCurveToPoint(path,NULL,50.0,275.0,
                          150.0,275.0,
                          150.0,120.0);
    CGPathAddCurveToPoint(path,NULL,150.0,275.0,
                          250.0,275.0,
                          250.0,120.0);    
    CGPathAddCurveToPoint(path,NULL,250.0,275.0,
                          350.0,275.0,
                          350.0,120.0);    
    CGPathAddCurveToPoint(path,NULL,350.0,275.0,
                          450.0,275.0,
                          450.0,120.0);

    CAKeyframeAnimation *
        animation = [CAKeyframeAnimation 
                     animationWithKeyPath:@"position"];

    [animation setPath:path];
    [animation setDuration:3.0];

    [animation setAutoreverses:YES];

    CFRelease(path);

    return animation;

}
Run Code Online (Sandbox Code Playgroud)