ios 如何为多个属性创建 CABasicAnimation?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10938223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create an CABasicAnimation for multiple properties?
提问by Mc-
I have this code to animate a CALayer element.
我有这个代码来为 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];
My question is, now everything works fine, I would like another attribute of the same element at the same time. I've seen you can do additive animations and stuff.
我的问题是,现在一切正常,我想同时拥有同一元素的另一个属性。我已经看到你可以做附加动画和其他东西。
My question is:
我的问题是:
- Is the additive attribute the best / only way to do that? (animating at once multiple properties of the same object at once )
- 附加属性是最好的/唯一的方法吗?(同时为同一对象的多个属性设置动画)
Thanks!
谢谢!
回答by David R?nnqvist
You can create an CAAnimationGroup
and customize the duration and timing function on it. Then you create all your CABasicAnimations
, set their to value and add them to the animation group. Finally, you add the animation group to the layer that you are animating.
您可以在其上创建CAAnimationGroup
并自定义持续时间和计时功能。然后创建所有的CABasicAnimations
,将它们设置为值并将它们添加到动画组中。最后,将动画组添加到要设置动画的层。
Here an example:
这里有一个例子:
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"];
回答by Aaron Zheng
let groupAnimation = CAAnimationGroup()
groupAnimation.beginTime = CACurrentMediaTime() + 0.5
groupAnimation.duration = 0.5
let scaleDown = CABasicAnimation(keyPath: "transform.scale")
scaleDown.fromValue = 3.5
scaleDown.toValue = 1.0
let rotate = CABasicAnimation(keyPath: "transform.rotation")
rotate.fromValue = .pi/10.0
rotate.toValue = 0.0
let fade = CABasicAnimation(keyPath: "opacity")
fade.fromValue = 0.0
fade.toValue = 1.0
groupAnimation.animations = [scaleDown,rotate,fade]
loginButton.layer.add(groupAnimation, forKey: nil)
This is for the newest update on swift (swift 3). Your code should include a object at the end, i.e. UIButton, UILabel, something that you can animate. In my code it was the loginButton (which was the title or name).
这是针对 swift (swift 3) 的最新更新。您的代码应该在末尾包含一个对象,即 UIButton、UILabel,您可以设置动画。在我的代码中,它是 loginButton(它是标题或名称)。
回答by Shrawan
In Swift-3we can use CAAnimationGroupas below :-
在Swift-3 中,我们可以使用CAAnimationGroup如下:-
let position = CAKeyframeAnimation(keyPath: "position")
position.values = [ NSValue.init(cgPoint: .zero) , NSValue.init(cgPoint: CGPoint(x: 0, y: -20)) , NSValue.init(cgPoint: .zero) ]
position.timingFunctions = [ CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) ]
position.isAdditive = true
position.duration = 1.2
let rotation = CAKeyframeAnimation(keyPath: "transform.rotation")
rotation.values = [ 0, 0.14, 0 ]
rotation.duration = 1.2
rotation.timingFunctions = [ CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) ]
let fadeAndScale = CAAnimationGroup()
fadeAndScale.animations = [ position, rotation]
fadeAndScale.duration = 1