How can I create an CABasicAnimation for multiple properties? How can I create an CABasicAnimation for multiple properties? ios ios

How can I create an CABasicAnimation for multiple properties?


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.

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 groupCAAnimationGroup *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.5groupAnimation.duration = 0.5let scaleDown = CABasicAnimation(keyPath: "transform.scale")scaleDown.fromValue = 3.5scaleDown.toValue = 1.0let rotate = CABasicAnimation(keyPath: "transform.rotation")rotate.fromValue = .pi/10.0rotate.toValue = 0.0let fade = CABasicAnimation(keyPath: "opacity")fade.fromValue = 0.0fade.toValue = 1.0groupAnimation.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).


In Swift-3 we can use CAAnimationGroup as below :-

        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