Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

id title brief sdk
Create an Animation Group
This recipe shows how to create an animation that groups multiple animations together using Core Animation.


Follow these steps to create the animation:

  • Add an image named sample.png to the project with a Build Action of Content.
  • Add the following using directives:
using CoreAnimation
using CoreGraphics;
using Foundation;
  • In a UIViewController subclass, declare a CALayer class variable from the CoreAnimation namespace.
CALayer layer;
  • In ViewDidLoad method, create the layer and set its content to be the image.
 layer = new CALayer ();
 layer.Bounds = new CGRect (0, 0, 50, 50);
 layer.Position = new CGPoint (100, 100);
 layer.Contents = UIImage.FromFile ("sample.png").CGImage;
 layer.ContentsGravity = CALayer.GravityResizeAspectFill;
  • Add the layer as a sub layer of the view’s layer.
View.Layer.AddSublayer (layer);
  • Override the ViewWillAppear method, add the following code to create the animations:
public override void ViewWillAppear (bool animated)
	base.ViewDidAppear (animated);

	//Creates basic moving animation
	var pt = layer.Position;
	layer.Position = new CGPoint (100, 300);
	var basicAnimation = CABasicAnimation.FromKeyPath ("position");
	basicAnimation.TimingFunction =
	basicAnimation.From = NSValue.FromCGPoint (pt);
	basicAnimation.To = NSValue.FromCGPoint (new CGPoint (100, 300));

	//Creates transformation animation
	layer.Transform = CATransform3D.MakeRotation ((float)Math.PI * 2, 0, 0, 1);
	var animRotate = (CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath ("transform");
	animRotate.Values = new NSObject[] {
		NSNumber.FromFloat (0f),
		NSNumber.FromFloat ((float)Math.PI / 2f),
		NSNumber.FromFloat ((float)Math.PI),
		NSNumber.FromFloat ((float)Math.PI * 2)};
	animRotate.ValueFunction = CAValueFunction.FromName (CAValueFunction.RotateX);

	//Adds the animations to a group, and adds the group to the layer
	var animationGroup = CAAnimationGroup.CreateAnimation ();
	animationGroup.Duration = 2;
	animationGroup.Animations = new CAAnimation[] { basicAnimation, animRotate };
	layer.AddAnimation (animationGroup, null);

Additional Information

The code creates 2 separate explicit animations. The first animation is a CABasicAnimation that animates the layer’s position. The second animation is a CAKeyFrameAnimation that rotates the layer. These animations are combined into a CAAnimationGroup that is added to the layer.