gpt4 book ai didi

ios - 动画绘制 CAShapeLayer 的填充

转载 作者:可可西里 更新时间:2023-11-01 02:59:10 25 4
gpt4 key购买 nike

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

例如,我想在屏幕上绘制一些文本,但我只能绘制文本的描边,而不能绘制填充。另一个例子,我有一个星形,我想为其填充动画。

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

谢谢!

最佳答案

大部分时间都是相同的代码,您只需为 CABasicAnimationfromValuetoValue 设置不同的值。我创建了一个类别,它返回一个 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 只需添加到 CAShapeLayer 中:

[_myShapeLayer addAnimation:returnedAnimation forKey:@"animKey"]

关于ios - 动画绘制 CAShapeLayer 的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5886679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com