gpt4 book ai didi

ios - 如何为多个属性创建 CABasicAnimation?

转载 作者:IT王子 更新时间:2023-10-29 07:46:22 26 4
gpt4 key购买 nike

我有这段代码可以为 CALayer 元素设置动画。

CABasicAnimation *makeBiggerAnim=[CABasicAnimation animationWithKeyPath:@"radius"];
makeBiggerAnim.duration=0.2;
makeBiggerAnim.fromValue=[NSNumber numberWithDouble:20.0];
makeBiggerAnim.toValue=[NSNumber numberWithDouble:40.0];
makeBiggerAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

我的问题是,现在一切正常,我想同时获得同一元素的另一个属性。我看到您可以制作附加动画和其他东西。

我的问题是:

  • 附加属性是最好的/唯一的方法吗? (同时为同一对象的多个属性设置动画)

谢谢!

最佳答案

您可以创建一个 CAAnimationGroup 并在其上自定义持续时间和计时功能。然后创建所有 CABasicAnimations,设置它们的值并将它们添加到动画组。最后,将动画组添加到要设置动画的图层。

举个例子:

CABasicAnimation *makeBiggerAnim=[CABasicAnimation animationWithKeyPath:@"cornerRadius"];
makeBiggerAnim.fromValue=[NSNumber numberWithDouble:20.0];
makeBiggerAnim.toValue=[NSNumber numberWithDouble:40.0];

CABasicAnimation *fadeAnim=[CABasicAnimation animationWithKeyPath:@"opacity"];
fadeAnim.fromValue=[NSNumber numberWithDouble:1.0];
fadeAnim.toValue=[NSNumber numberWithDouble:0.0];

CABasicAnimation *rotateAnim=[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotateAnim.fromValue=[NSNumber numberWithDouble:0.0];
rotateAnim.toValue=[NSNumber numberWithDouble:M_PI_4];

// Customizing the group with duration etc, will apply to all the
// animations in the group
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 0.2;
group.repeatCount = 3;
group.autoreverses = YES;
group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
group.animations = @[makeBiggerAnim, fadeAnim, rotateAnim];

[myLayer addAnimation:group forKey:@"allMyAnimations"];

关于ios - 如何为多个属性创建 CABasicAnimation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10938223/

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