iOS Guides | Samples

MonoTouch.CoreAnimation Namespace

The CoreAnimation namespace provides access to the underlying animation framework that powers UIKit.


CoreAnimation is at the core of the iPhone UI. The APIs in this namespace give you access to the underlying animation framwork that powers the UIKit.

The UIKit controls are implemented on top of CoreAnimation which interfaces directly with the OpenGL and CoreGraphics to provide hardware accelerated rendering.

Each UIView is backed by a CALayer which is accessed through the UIView.Layer property. When you draw by overriding the UIView.Draw(RectangleF) method, you are drawing into the CoreAnimation layer.

Just like UIView's can contain other UIViews, CALayers can contain other CALayer instances. You can insert child layers into a layer by calling CALayer.AddSublayer(CALayer), CALayer.InsertSublayer(CALayer,int), CALayer.InsertSublayerBelow(CALayer,CALayer), CALayer.InsertSublayerAbove(CALayer,CALayer) or remove the layer by using CALayer.RemoveFromSuperLayer. In addition, there are various kinds of CALayers provided by the operating system and you can create your own by subclassing one of the system provided layers: CALayer, CATiledLayer, CATextLayer, CAScrollLayer, CAReplicatorLayer, CAShapeLayer, CAGradientLayer, CATransformLayer, CAEAGLLayer and CAEmitterLayer.

Layers will retain the contents that you draw into them, unlike other toolkits it is not necessary to implement a repaint method to respond to region-exposed events. If you want to update the contents of the layer, you should call the CALayer.SetNeedsDisplay method which will trigger a call to the CALayer.DrawInContext(CGContext) method which you can override.

You can customize layer rendering by setting the CALayer.Delegate property of your layer to point to an instance of a CoreAnimation.CALayerDelegate subclass.

You can apply 3D transformations to your layers by setting the CALayer.Transform property and you can also control the 3D transform applied to sublayers by setting the CALayer.SublayerTransform property. If you use the SublayerTransform, you can also use the CALayer.ZPosition property to give it a Z axis position. This is helpful to do perspective renderings.

Layers provide the hardware accelerated components necessary for CoreAnimation to do its job efficiently. On top of this functionality, CoreAnimation provides a set of APIs to animate layers.

Prior to iOS 4, animations were specified as transactions: application developers would bracket the specification of their animations between calls to UIView.BeginAnimations and UIView.CommitAnimations. Reacting to animation events (such as continuation after the animation finishes) requires the use of a delegate object and custom selectors. This technique is still available, but Apple recommends the use of "block-based" animations in modern apps. In C# terminology, these would be called "delegate-based" animations, where the delegate (or anonymous function) is of type NSAction. Additionally, Xamarin.iOS provides asynchronous wrappers for the commonly-used animation functions, so application developers can use C# 5+'s async-await facilities.

The following example shows the different techniques:

C# Example

//Transaction-based (recommended only for iOS < 4)
imgView.Layer.Position = newPosition;
UIView.SetAnimationDelegate (this);
UIView.SetAnimationDidStopSelector (new Selector ("positionAnimationFinished:"));
void SlideStopped ()
    Console.WriteLine("Animation finished; logic continues here");


C# Example

//Block-based, with animations in lambda, continuation as delegate (either technique works in either position)
UIView.Animate(2.0, () => imgView.Layer.Position = newPosition, SlideStopped);
void SlideStopped() { 
     Console.WriteLine("Animation finished; logic continues here");


C# Example

async void MyAnimateAsync {
    await UIView.AnimateAsync(2.0, () => imgView.Layer.Position = newPosition);
    Console.WriteLine("Animation finished; logic continues here");

These UIKit-based techniques should satisfy most animation use-cases (also, Sprite Kit provides both animation and physics-modeling appropriate for high frame-rate use-cases such as games). However, in addition to these UIKit-based techniques, application developers who create their own CALayers have access to lower-level animation techniques: Implicit Animations and Explicit Animations.

N.B.: Layer animations are disabled in UIViews except in UIView animation blocks. (See discussion below.)

Implicit Animations take place when app developers change one or more of the properties in a layer and CoreAnimation will apply those changes gradually by interpolating the values from the current value to the new value over a predetermined period of time (unless configured, the animations will take 0.25 seconds to execute).

C# Example

// The following method sets the opacity to zero on the image's Layer
// and will trigger a 0.25 animation to vanish the image by setting the
// opacity to zero
void HideImage (UIImageView image)
    view.Layer.Opacity = 0;

Application developers who want more control can use Explicit Animation. To do this, they create an instance of one of the animation classes CAPropertyAnimation, CATransition, CAAnimationGroup, CABasicAnimation or CAKeyFrameAnimation. The animation is attached to a layer, by calling the CALayer.AddAnimation(CAAnimation,string) method.

Unlike implicit animations which happen in reaction to changes in the layer's properties, explicit animations do not alter the properties of your objects. Instead they alter the properties of a copy of your scene graph stored in the CALayer.PresentationLayer. This means that any changes that you make to the objects as part of an explicit animation are not permanent. Once the animation is finished, the objects will be rendered with the values that are still in the model.

C# Example

// Notice that we set the final position for the layer before we start
// animating from 0 to 120 since this is an explicit animation and we
// do not want to see the object "jump" back to 0, 0 at the end of
// the animation
layer.Position = new PointF (0, 120);
var positionAnimation = (CAKeyFrameAnimation) CAKeyFrameAnimation.FromKeyPath ("position.y");
positionAnimation.Values = new NSNumber [] { 0, 30, 60, 120 };
layer.AddAnimation (positionAnimation, "myAnimation");
Layer Animations of UIViews

Layer-based animations are disabled by UIViews except within UIView animation blocks. Layer-based animations within such blocks ignore the blocks' duration and operate at their own specified duration, either the implicit default of 0.25 seconds or an explicit length. This is shown in the following example, in which the UIView animation block's duration is 1.0, but in actuality, the layer-based implicit opacity animation ends in 0.25 seconds and the re-positioning runs for 10 seconds.

C# Example

UIView.AnimateAsync(1.0, () => {
	imgView.Layer.Opacity = 0.0f;

	var theAnim = CABasicAnimation.FromKeyPath("position");
	theAnim.From = NSObject.FromObject(firstPosition);
	theAnim.To =  NSObject.FromObject(secondPosition);
	theAnim.Duration = 10.0;

	imgView.Layer.AddAnimation(theAnim, "AnimateFrame");


CAActionAn interface implemented by objects that participate in animations coordinated by a CALayer.
CAAnimationBase class for animations.
CAAnimationDelegateDelegate class for the CAAnimation class.
CAAnimationGroupGroups and orchestrates multiple animations.
CAAnimationStateEventArgsProvides data for the CAAnimationStateEventArgs.AnimationStopped event.
CABasicAnimationSingle keyframe based animations.
CADisplayLinkSynchronization object between your animations and the display refresh.
CAEAGLLayerLayer used to render OpenGL content.
CAEdgeAntialiasingMaskFlags used to determine what side of a layer should be antialiased.
CAEmitterBehaviorDefines the behavior of a particle system emitter.
CAEmitterCellA source of particles emitted by a MonoTouch.CoreAnimation.CAEmitterLayer instance.
CAEmitterLayerA particle-system emitter. Particle types are defined by MonoTouch.CoreAnimation.CAEmitterCell.
CAFillModeConstants used for the FillMode property in CAAnimation and CALayer, used to control the behavior of objects once the animation has completed.
CAGradientLayerLayer that renders a gradient over its background.
CAKeyFrameAnimationKeyframe-based animation support.
CALayerLayers hold the images that are rendered into the screen.
CALayerDelegateDelegate class for the CALayer.
CALayerDelegate_ExtensionsExtension methods to the ICALayerDelegate interface to support all the methods from the CALayerDelegate protocol.
CAMediaTimingProvides a hierarchical timing system, with support for repetition and sequencing.
CAMediaTimingFunctionDefines the pacing of an animation.
CAMetalLayerA CALayer that is rendered using Metal functions.
CAPropertyAnimationAn animation that can animate object properties.
CAReplicatorLayerA layer that replicates an existing layer, with some attributes (color, transform) altered.
CAScrollEnumerates scrolling directions.
CAScrollExtensionsExtension methods for CAScroll.
CAScrollLayerLayer used to show portions of another layer.
CAShapeLayerDraws a bezier curve and composes the result with its first sublayer.
CASpringAnimationA spring animation with stiffness, mass, and damping.
CATextLayerSimple text layour and rendering of regular or attributed text.
CATiledLayerLayer whose content can be provided asynchronously, and with multiple levels of detail.
CATransactionFramework to synchronize multiple transformation operations.
CATransform3D3D transformation.
CATransformLayer3D compositing layer.
CATransitionTransition animations for a layer.
CAValueFunctionClass used to apply functions to property values during an animation.
ICAActionInterface representing the required methods (if any) of the protocol CAAction.
ICALayerDelegateInterface representing the required methods (if any) of the protocol CALayerDelegate.
ICAMediaTimingInterface representing the required methods (if any) of the protocol CAMediaTiming.
ICAMetalDrawableInterface that defines a protocol for a display buffer at the metal layer.