gpt4 book ai didi

ios - 如何通过自定义渐变更改 strokeColor

转载 作者:行者123 更新时间:2023-11-29 11:00:01 25 4
gpt4 key购买 nike

我想为 CAShapeLayer 的 strokeColor 设置动画,但在 CABasicAnimation 中我有两个值(from 和 to)。动画火时是否只支持两种颜色?例如,开始时我有 strokeColor = [UIColor blueColor].CGColor;然后

CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
colorAnimation.duration = 3.0; // "animate over 3 seconds or so.."
colorAnimation.repeatCount = 1.0; // Animate only once..
colorAnimation.removedOnCompletion = NO; // Remain stroked after the animation..
colorAnimation.fillMode = kCAFillModeForwards;
colorAnimation.toValue = (id)[UIColor redColor].CGColor;
colorAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

在中途我有一个深紫色,但我需要,例如,黄色。

是否可以向 CABasicAnimation 添加自定义渐变?

最佳答案

我不认为你可以用 CABasicAnimation 做到这一点,但你可以使用 CAKeyframeAnimation为动画设置中间值:

CAKeyframeAnimation *colorAnimation = [CAKeyframeAnimation animationWithKeyPath:@"strokeColor"];
colorAnimation.values = @[(id)[[UIColor blueColor] CGColor],
(id)[[UIColor yellowColor] CGColor],
(id)[[UIColor redColor] CGColor]];
colorAnimation.duration = 3.0; // "animate over 3 seconds or so.."
colorAnimation.repeatCount = 1.0; // Animate only once..
colorAnimation.removedOnCompletion = NO; // Remain stroked after the animation..
colorAnimation.fillMode = kCAFillModeForwards;
colorAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

如果你想要一种“跨越光谱”的感觉,你可以这样做:

colorAnimation.values = @[(id)[[UIColor blueColor] CGColor],
(id)[[UIColor greenColor] CGColor],
(id)[[UIColor yellowColor] CGColor],
(id)[[UIColor orangeColor] CGColor],
(id)[[UIColor redColor] CGColor]];

或者,如果您想要更多简单的蓝色到红色,但又要避免深紫色,您可以这样做:

colorAnimation.values = @[(id)[[UIColor blueColor] CGColor],
(id)[[UIColor colorWithRed:0.9 green:0.0 blue:0.9 alpha:1.0] CGColor],
(id)[[UIColor redColor] CGColor]];

很多选项。

关于ios - 如何通过自定义渐变更改 strokeColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366789/

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