MonoTouch.CoreAnimation Namespace

CoreAnimation Framework to animate your user interface.

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 MonoTouch.UIKit.UIView is backed by a MonoTouch.CoreAnimation.CALayer which is accessed through the MonoTouch.UIKit.UIView.Layer property. When you draw by overriding the MonoTouch.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 MonoTouch.CoreAnimation.CALayer.AddSublayer(MonoTouch.CoreAnimation.CALayer), MonoTouch.CoreAnimation.CALayer.InsertSublayer(MonoTouch.CoreAnimation.CALayer, int), MonoTouch.CoreAnimation.CALayer.InsertSublayerBelow(MonoTouch.CoreAnimation.CALayer, MonoTouch.CoreAnimation.CALayer), MonoTouch.CoreAnimation.CALayer.InsertSublayerAbove(MonoTouch.CoreAnimation.CALayer, MonoTouch.CoreAnimation.CALayer) or remove the layer by using MonoTouch.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: MonoTouch.CoreAnimation.CALayer, MonoTouch.CoreAnimation.CATiledLayer, MonoTouch.CoreAnimation.CATextLayer, MonoTouch.CoreAnimation.CAScrollLayer, MonoTouch.CoreAnimation.CAReplicatorLayer, MonoTouch.CoreAnimation.CAShapeLayer, MonoTouch.CoreAnimation.CAGradientLayer, MonoTouch.CoreAnimation.CATransformLayer, MonoTouch.CoreAnimation.CAEAGLLayer and MonoTouch.CoreAnimation.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 MonoTouch.CoreAnimation.CALayer.SetNeedsDisplay() method which will trigger a call to the MonoTouch.CoreAnimation.CALayer.DrawInContext(MonoTouch.CoreGraphics.CGContext) method which you can override.

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

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

C# Example

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

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);
//...etc...
void SlideStopped() { 
     Console.WriteLine("Animation finished; logic continues here");
}
          

Asynchronous

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 MonoTouch.CoreAnimation.CALayers have access to lower-level animation techniques: Implicit Animations and Explicit Animations.

N.B.: Layer animations are disabled in MonoTouch.UIKit.UIViews except in MonoTouch.UIKit.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 MonoTouch.CoreAnimation.CAPropertyAnimation, MonoTouch.CoreAnimation.CATransition, MonoTouch.CoreAnimation.CAAnimationGroup, MonoTouch.CoreAnimation.CABasicAnimation or MonoTouch.CoreAnimation.CAKeyFrameAnimation. The animation is attached to a layer, by calling the MonoTouch.CoreAnimation.CALayer.AddAnimation(MonoTouch.CoreAnimation.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 MonoTouch.CoreAnimation.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 MonoTouch.UIKit.UIViews

Layer-based animations are disabled by MonoTouch.UIKit.UIViews except within MonoTouch.UIKit.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 MonoTouch.UIKit.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");
});
          

Classes

TypeReason
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 MonoTouch.CoreAnimation.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 MonoTouch.CoreAnimation.ICALayerDelegate interface to support all the methods from the MonoTouch.CoreAnimation.CALayerDelegate protocol.
CAMediaTimingProvides a hierarchical timing system, with support for repetition and sequencing.
CAMediaTimingFunctionDefines the pacing of an animation.
CAMetalLayerA MonoTouch.CoreAnimation.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.
CAScrollLayerLayer used to show portions of another layer.
CAShapeLayerDraws a bezier curve and composes the result with its first sublayer.
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 MonoTouch.CoreAnimation.CAAction.
ICALayerDelegateInterface representing the required methods (if any) of the protocol MonoTouch.CoreAnimation.CALayerDelegate.
ICAMediaTimingDocumentation for this section has not yet been entered.
ICAMetalDrawableDocumentation for this section has not yet been entered.