ios animation core-animation caanimation

ios - ¿Cómo puedo crear una CABasicAnimation para propiedades múltiples?



core-animation caanimation (3)

Tengo este código para animar un elemento 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];

Mi pregunta es, ahora todo funciona bien, me gustaría otro atributo del mismo elemento al mismo tiempo. He visto que puedes hacer animaciones aditivas y esas cosas.

Mi pregunta es:

  • ¿El atributo aditivo es la mejor / única forma de hacerlo? (animando a la vez múltiples propiedades del mismo objeto a la vez)

¡Gracias!


En Swift-3 podemos usar CAAnimationGroup de la siguiente manera:

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


Puede crear un CAAnimationGroup y personalizar la duración y la función de tiempo en él. A continuación, cree todas sus CABasicAnimations , establezca sus valores y agréguelos al grupo de animación. Finalmente, agrega el grupo de animación a la capa que está animando.

Aquí un ejemplo:

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"];


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)

Esto es para la última actualización en swift (swift 3). Su código debe incluir un objeto al final, es decir, UIButton, UILabel, algo que puede animar. En mi código, era el loginButton (que era el título o el nombre).