CoreAnimation Namespace
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 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) 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 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-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"); });
Classes
Type | Reason |
---|---|
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. |
CAAnimationGroup | Groups and orchestrates multiple animations. |
CAAnimationStateEventArgs | Provides data for the CAAnimationStateEventArgs.AnimationStopped event. |
CABasicAnimation | Single keyframe based animations. |
CAContentsFormat | |
CAContentsFormatExtensions | |
CACornerMask | |
CADisplayLink | Synchronization object between your animations and the display refresh. |
CAEAGLLayer | Layer used to render OpenGL content. |
CAEdgeAntialiasingMask | Flags used to determine what side of a layer should be antialiased. |
CAEmitterBehavior | Defines the behavior of a particle system emitter. |
CAEmitterCell | A source of particles emitted by a CoreAnimation.CAEmitterLayer instance. |
CAEmitterLayer | A particle-system emitter. Particle types are defined by CoreAnimation.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. |
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. |
CAPropertyAnimation | An animation that can animate object properties. |
CAReplicatorLayer | A layer that replicates an existing layer, with some attributes (color, transform) altered. |
CAScroll | Enumerates scrolling directions. |
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. |
CATiledLayer | Layer whose content can be provided asynchronously, and with multiple levels of detail. |
CATransaction | Framework to synchronize multiple transformation operations. |
CATransform3D | 3D transformation. |
CATransformLayer | 3D compositing layer. |
CATransition | Transition animations for a layer. |
CAValueFunction | Class used to apply functions to property values during an animation. |
ICAAction | Interface representing the required methods (if any) of the protocol CAAction. |
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. |