CoreAnimation Namespace

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

Classes

CAAction

An interface implemented by objects that participate in animations coordinated by a CALayer.

CAAnimation

Base class for animations.

CAAnimationDelegate

Delegate class for the CAAnimation class.

CAAnimationDelegate_Extensions

Extension methods to the CAAnimationDelegate class.

CAAnimationGroup

Groups and orchestrates multiple animations.

CAAnimationStateEventArgs

Provides data for the E:CoreAnimation.CAAnimationStateEventArgs.AnimationStopped event.

CABasicAnimation

Single keyframe based animations.

CAConstraint
CAConstraintLayoutManager
CAContentsFormatExtensions
CAEAGLLayer

Layer used to render OpenGL content.

CAEmitterBehavior

Defines the behavior of a particle system emitter.

CAEmitterCell

A source of particles emitted by a CAEmitterLayer instance.

CAEmitterLayer

A particle-system emitter. Particle types are defined by CAEmitterCell.

CAFillMode

Constants used for the FillMode property in CAAnimation and CALayer, used to control the behavior of objects once the animation has completed.

CAGradientLayer

Layer that renders a gradient over its background.

CAGradientLayerTypeExtensions
CAKeyFrameAnimation

Keyframe-based animation support.

CALayer

Layers hold the images that are rendered into the screen.

CALayerDelegate

Delegate class for the CALayer.

CALayerDelegate_Extensions

Extension methods to the ICALayerDelegate interface to support all the methods from the CALayerDelegate protocol.

CAMediaTiming

Provides a hierarchical timing system, with support for repetition and sequencing.

CAMediaTimingFunction

Defines the pacing of an animation.

CAMetalLayer

A CALayer that is rendered using Metal functions.

CAOpenGLLayer
CAPropertyAnimation

An animation that can animate object properties.

CARenderer
CARendererOptions
CAReplicatorLayer

A layer that replicates an existing layer, with some attributes (color, transform) altered.

CAScrollExtensions

Extension methods for CAScroll.

CAScrollLayer

Layer used to show portions of another layer.

CAShapeLayer

Draws a bezier curve and composes the result with its first sublayer.

CASpringAnimation

A spring animation with stiffness, mass, and damping.

CATextLayer

Simple text layour and rendering of regular or attributed text.

CATextLayerAlignmentModeExtensions
CATextLayerTruncationModeExtensions
CATiledLayer

Layer whose content can be provided asynchronously, and with multiple levels of detail.

CATransaction

Framework to synchronize multiple transformation operations.

CATransformLayer

3D compositing layer.

CATransition

Transition animations for a layer.

CAValueFunction

Class used to apply functions to property values during an animation.

Structs

CATransform3D

3D transformation.

Interfaces

ICAAction

Interface representing the required methods (if any) of the protocol CAAction.

ICAAnimationDelegate

Delegate for responding to animation lifecycle events.

ICALayerDelegate

Interface representing the required methods (if any) of the protocol CALayerDelegate.

ICAMediaTiming

Interface representing the required methods (if any) of the protocol CAMediaTiming.

ICAMetalDrawable

Interface that defines a protocol for a display buffer at the metal layer.

Enums

CAAutoresizingMask
CAConstraintAttribute
CAContentsFormat
CACornerMask
CAEdgeAntialiasingMask

Flags used to determine what side of a layer should be antialiased.

CAGradientLayerType
CAScroll

Enumerates scrolling directions.

CATextLayerAlignmentMode
CATextLayerTruncationMode

Remarks

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 Layer property. When you draw by overriding the M:UIKit.UIView.Draw(System.Drawing.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 AddSublayer(CALayer), M:CoreAnimation.CALayer.InsertSublayer(CoreAnimation.CALayer,int), InsertSublayerBelow(CALayer, CALayer), InsertSublayerAbove(CALayer, CALayer) or remove the layer by using M:CoreAnimation.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 M:CoreAnimation.CALayer.SetNeedsDisplay() method which will trigger a call to the DrawInContext(CGContext) method which you can override.

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

You can apply 3D transformations to your layers by setting the Transform property and you can also control the 3D transform applied to sublayers by setting the SublayerTransform property. If you use the SublayerTransform, you can also use the 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 M:UIKit.UIView.BeginAnimations* and M:UIKit.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 T:Foundation.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:


//Transaction-based (recommended only for iOS < 4)
UIView.BeginAnimations("transactional");
UIView.SetAnimationDuration(2.0);
imgView.Layer.Position = newPosition;
UIView.SetAnimationDelegate (this);
UIView.SetAnimationDidStopSelector (new Selector ("positionAnimationFinished:"));
UIView.CommitAnimations();
//...etc...
[Export("positionAnimationFinished:")]
void SlideStopped ()
{
    Console.WriteLine("Animation finished; logic continues here");
}

Block-based


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

Asynchronous


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).


//
// 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 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 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.


//
// 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.


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

});