本文介绍了CGPath动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need some sample code where I can animate a curve/arc path which should draw a full circle as animated?

任何输入都会受到赞赏。

Any input will be appreciated.

谢谢,
星期六

Thanks,Sat

推荐答案

更新:意识到有一个更好的解决方案。只需创建一个圆形路径并设置CAShapeLayer的strokeEnd属性从0.0f到1.0f即可。看看Ole的以下答案:

UPDATE: Realizing there is a better solution to this problem. Just create a circular path and animate the strokeEnd property of a CAShapeLayer from 0.0f to 1.0f. Take a look at this answer from Ole: Drawing a path with CAKeyFrameAnimation on iPhone

原始答案

您可以使用CAKeyframeAnimation并创建一个核心图形路径。这段代码以抛物线形为图层设置动画,但是您可以对其进行修改以绘制一个圆。

You can use a CAKeyframeAnimation and create a Core Graphics path. This code animates a layer in a parabolic pattern, but you can adapt it to draw a circle.

- (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;

}

这篇关于CGPath动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:42