动画绘制CAShapeLayer的填充

时间:2011-05-04 16:28:34

标签: ios animation core-animation calayer cashapelayer

我一直在玩这个伟大的文章http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer中描述的使用CAShapeLayer绘制路径,但我想知道是否有办法为图层的填充设置动画。

例如,我有一些我想要在屏幕上绘制的文字,但我只能绘制文本的笔划而不是填充。另一个例子,我有一个星形,我希望动画它被填充。

使用CAShapeLayer或其他对象可以吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

大部分时间都是相同的代码,您只需为fromValue的{​​{1}}和toValue设置不同的值即可。我创建了一个类别,它返回CABasicAnimation

StrokeEnd的动画

CABasicAnimation

fillColor的动画

+ (CABasicAnimation *)animStrokeEndWithDuration:(CGFloat)dur
                                       delegate:(id)target{
    CABasicAnimation *animLine = 
     [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    [animLine setDuration:dur];
    [animLine setFromValue:[NSNumber numberWithFloat:0.0f]];
    [animLine setToValue:[NSNumber numberWithFloat:1.0f]];
    [animLine setRemovedOnCompletion:NO];
    [animLine setFillMode:kCAFillModeBoth];
    [animLine setDelegate:target];
    [animLine setTimingFunction:
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    return animLine;
}

返回的+ (CABasicAnimation *)animFillColorWithDur:(CGFloat)dur startCol:(UIColor *)start endColor:(UIColor *)end delegate:(id)target{ CABasicAnimation *animFill = [CABasicAnimation animationWithKeyPath:@"fillColor"]; [animFill setDuration:dur]; [animFill setFromValue:(id)start.CGColor]; [animFill setToValue:(id)end.CGColor]; [animFill setRemovedOnCompletion:NO]; [animFill setDelegate:target]; [animFill setFillMode:kCAFillModeBoth]; return animFill; } 只需添加到CABasicAnimation

CAShapeLayer

答案 1 :(得分:0)

是的,这是可能的。

CAShapeLayers有一个可动画的fillColor属性。

它的工作方式与改变strokeEnd / strokeStart的方式相同,就像你已经完成动画一样。