我一直在尝试使用 CAShapeLayer 绘制路径,如这篇很棒的文章http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer中所述,但我是想知道是否有一种方法可以为图层的填充设置动画。
例如,我想在屏幕上绘制一些文本,但我只能绘制文本的笔触而不是填充。另一个例子,我有一个星形,我想用动画填充它。
这可能使用 CAShapeLayer 或其他对象吗?
谢谢!
我一直在尝试使用 CAShapeLayer 绘制路径,如这篇很棒的文章http://oleb.net/blog/2010/12/animating-drawing-of-cgpath-with-cashapelayer中所述,但我是想知道是否有一种方法可以为图层的填充设置动画。
例如,我想在屏幕上绘制一些文本,但我只能绘制文本的笔触而不是填充。另一个例子,我有一个星形,我想用动画填充它。
这可能使用 CAShapeLayer 或其他对象吗?
谢谢!
它大部分时间是相同的代码,你只需要为你的fromValue
和设置不同的值。我创建了一个返回我的类别:toValue
CABasicAnimation
CABasicAnimation
StrokeEnd 的动画
+ (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
只需添加到 a CAShapeLayer
:
[_myShapeLayer addAnimation:returnedAnimation forKey:@"animKey"]
对的,这是可能的。
CAShapeLayers 有一个可动画的 fillColor 属性。
它的工作方式与更改 strokeEnd / strokeStart 的方式相同,就像您已经对动画所做的那样。