CALayer Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Layers hold the images that are rendered into the screen.
[Foundation.Register("CALayer", true)]
public class CALayer : Foundation.NSObject, CoreAnimation.ICAMediaTiming, Foundation.INSSecureCoding, IDisposable
[<Foundation.Register("CALayer", true)>]
type CALayer = class
inherit NSObject
interface ICAMediaTiming
interface INativeObject
interface IDisposable
interface INSCoding
interface INSSecureCoding
- Inheritance
- Derived
- Attributes
- Implements
Remarks
CALayers hold the image content that is rendered into the screen. They encapsulate position, styling, size and transformation components. They also implement the CAMediaTiming methods which allows them to participate in animations.
There are several subclasses of CALayer that developers can use: CAEmitterLayer, CAGradientLayer, CAReplicatorLayer, CAScrollLayer, CAShapeLayer, CATextLayer, CATiledLayer, CATransformLayer.
Layer Content
There are three ways of providing content to a layer: subclassing the layer class and overriding the draw methods, using a layer delegate to implement the drawing or assigning a static image to the layer.
To set the contents of the layer with a static image or from one of the rendering approaches, app devs must assign a CGImage to the Contents property. For static content, they can just assign this property and the changes will be reflected directly.
Contents by Subclassing CALayer
If you choose to subclass the CALayer class, you can either subclass the Display() method which is then requires to set the Contents property or you can override the DrawInContext(CGContext) method which provides you with a graphics context that you can use to render into the display.
// Overriding DrawInContext
public class DemoLayer : CALayer {
public override void DrawInContext (CGContext context)
{
base.DrawInContext (context);
// Fill in circle
context.SetFillColor (Color);
context.SetShadowWithColor (CGSize.Empty, 10.0f, glowColor);
context.EOFillPath();
}
}
// Overriding Display
public class DemoLayer2 : CALayer {
CGImage image = UIImage.FromBundle ("demo.png").CGImage;
public override void Display ()
{
Contents = image;
}
}
Contents by Providing a CALayerDelegate
This approach can be used if the developer does not want to change the class used for their CALayer rendering, and all they need to do is assign the Delegate property to an instance of a subclass of CALayerDelegate where they either override the DisplayLayer(CALayer) method in which they must set the Contents property, or they override the DrawLayer(CALayer, CGContext) method and provide their own rendering code there.
// Overriding DisplayLayer
public class DemoLayerDelegate : CALayerDelegate {
CGImage image = UIImage.FromBundle ("demo.png").CGImage;
public override void DisplayLayer (CALayer layer)
{
layer.Contents = image;
}
}
// Overriding DrawLayer
public class DemoLayerDelegate2 : CALayerDelegate {
public override DrawLayer (CALayer layer, CGContext context)
{
// Fill in circle
context.SetFillColor (Color);
context.SetShadowWithColor (CGSize.Empty, 10.0f, glowColor);
context.EOFillPath();
}
}
// To use the code:
void SetupViews (UIView view, UIView view2)
{
view.Layer.Delegate = new DemoLayerDelegate ();
view2.Layer.Delegate = new DemoLayerDelegate2 ();
}
Using Custom Layers with your UIViews or NSViews.
On iOS, every UIView automatically has a CALayer associated with it. When you want to use one of the CALayer subclasses as your UIView's backing layer, you need to add the following code snippet to your class:
class MyView : UIView {
//
// This instructs the runtime that whenever a MyView is created
// that it should instantiate a CATiledLayer and assign that to the
// UIView.Layer property
//
[Export ("layerClass")]
public static Class LayerClass () {
return new Class (typeof (CATiledLayer));
}
}
If you want to subclass the CALayer class, you must provide a constructor that takes a CALayer and is annotated with an [Export ("initWithLayer:")] attribute. When you do this, you should also override the Clone(CALayer) as these two are used to create copies of your layer state on demand in response to CoreAnimation creating a mirror of your object hierarchy if anyone accesses the PresentationLayer property.
public class MyLayer : CALayer {
UIColor FirstColor, SecondColor;
//
// Invoked by CoreAnimation if it needs to create a copy of your layer
// with a specific state in response to the user fetching the PresentationLayer
// property
//
[Export ("initWithLayer:")]
public MyLayer (Mylayer other) : base (layer)
{
// Do nothing, since we override Clone, but we could
// just clone the data here as well if we wanted to.
}
//
// This is the constructor you would use to create your new CALayer
public MyLayer (UIColor firstColor, UIColor secondColor)
{
FirstColor = firstColor;
SecondColor = secondColor;
}
// We must copy our own state here from the original layer
public override void Clone (CALayer _other)
{
MyLayer other = (MyLayer) _other;
FirstColor = other.FirstColor;
SecondColor = other.SecondColor;
}
}
Constructors
| Name | Description |
|---|---|
| CALayer() |
Creates a new CALayer with default values. |
| CALayer(CALayer) |
This method must be implemented by derived classes to make a copy of the original layer. |
| CALayer(NativeHandle) |
A constructor used when creating managed representations of unmanaged objects. Called by the runtime. |
| CALayer(NSCoder) |
A constructor that initializes the object from the data stored in the unarchiver object. |
| CALayer(NSObjectFlag) |
Constructor to call on derived classes to skip initialization and merely allocate the object. |
Properties
| Name | Description |
|---|---|
| AccessibilityAttributedUserInputLabels | (Inherited from NSObject) |
| AccessibilityRespondsToUserInteraction | (Inherited from NSObject) |
| AccessibilityTextualContext | (Inherited from NSObject) |
| AccessibilityUserInputLabels | (Inherited from NSObject) |
| Actions |
NSDictionary containing the layer's actions. |
| AffineTransform |
The affine version of the layer's Transform. |
| AllowsEdgeAntialiasing |
Whether edge antialiasing is allowed. |
| AllowsGroupOpacity |
Whether group opacity is allowed. |
| AnchorPoint |
The anchor point for the Bounds rectangle. |
| AnchorPointZ |
The anchor point for the Bounds, defined along the Z axis. |
| AnimationKeys |
Identifiers for the animations associated with this layer. |
| AutoresizingMask | |
| AutoReverses | |
| BackgroundColor |
The background color of the layer. |
| BackgroundFilters |
An array of Core Image filters that are applied to the content behind this layer. |
| BeginTime | |
| BorderColor |
The color of the border of the layer. |
| BorderWidth |
The width of the border of the layer. |
| Bounds | |
| Class | (Inherited from NSObject) |
| ClassHandle |
The Objective-C class handle for this class. |
| CompositingFilter |
A Core Image filter that is used for compositing the layer and the content behind it. |
| Constraints | |
| Contents |
The contents of this layer, as a CGImage. |
| ContentsAreFlipped |
Whether the layer's contents are implicitly flipped when the layer is rendered. |
| ContentsCenter | |
| ContentsFormat |
Gets the contents format for the layer. |
| ContentsGravity | |
| ContentsHeadroom | |
| ContentsRect | |
| ContentsScale |
The scale factor applied to the layer. |
| CornerCurve | |
| CornerRadius |
The radius used when drawing rounded corners. |
| DebugDescription | (Inherited from NSObject) |
| Delegate |
An instance of the CoreAnimation.ICALayerDelegate model class which acts as the class delegate. |
| Description | (Inherited from NSObject) |
| DoubleSided |
Whether the layer draws its content when facing away from the viewer. |
| DrawsAsynchronously |
Whether drawing commands are deferred and processed asynchronously on a background thread. |
| Duration | |
| EdgeAntialiasingMask | |
| ExposedBindings | (Inherited from NSObject) |
| FillMode | |
| FilterLinear |
Represents the value associated with the constant kCAFilterLinear |
| FilterNearest |
Represents the value associated with the constant kCAFilterNearest |
| Filters |
An array of Core Image filters applied to the content of this layer and its sublayers. |
| FilterTrilinear |
Represents the value associated with the constant kCAFilterTrilinear |
| Frame | |
| GeometryFlipped | |
| GravityBottom |
Represents the value associated with the constant kCAGravityBottom |
| GravityBottomLeft |
Represents the value associated with the constant kCAGravityBottomLeft |
| GravityBottomRight |
Represents the value associated with the constant kCAGravityBottomRight |
| GravityCenter |
Represents the value associated with the constant kCAGravityCenter |
| GravityLeft |
Represents the value associated with the constant kCAGravityLeft |
| GravityResize |
Represents the value associated with the constant kCAGravityResize |
| GravityResizeAspect |
Represents the value associated with the constant kCAGravityResizeAspect |
| GravityResizeAspectFill |
Represents the value associated with the constant kCAGravityResizeAspectFill |
| GravityRight |
Represents the value associated with the constant kCAGravityRight |
| GravityTop |
Represents the value associated with the constant kCAGravityTop |
| GravityTopLeft |
Represents the value associated with the constant kCAGravityTopLeft |
| GravityTopRight |
Represents the value associated with the constant kCAGravityTopRight |
| Handle |
Handle (pointer) to the unmanaged object representation. (Inherited from NSObject) |
| Hidden |
Whether the layer is not displayed. |
| IsDirectBinding | (Inherited from NSObject) |
| IsProxy | (Inherited from NSObject) |
| LayoutManager | |
| MagnificationFilter | |
| Mask |
An optional layer, the alpha channel of which is used to mask the contents of this layer. |
| MaskedCorners | |
| MasksToBounds |
Whether sublayers are clipped to the Bounds of this layer. |
| MinificationFilter | |
| MinificationFilterBias | |
| ModelLayer | |
| Name | |
| NeedsDisplay |
Whether the layer has been marked as requiring an update. |
| NeedsDisplayOnBoundsChange | |
| OnOrderIn |
Represents the value associated with the constant kCAOnOrderIn |
| OnOrderOut |
Represents the value associated with the constant kCAOnOrderOut |
| Opacity |
The transparency of the layer, in the range 0 (transparent) to 1.0 (opaque). |
| Opaque |
Whether the layer and its contents are completely opaque. |
| Position |
The layer's position in the coordinate space of its SuperLayer. |
| PreferredDynamicRange | |
| PresentationLayer | |
| RasterizationScale | |
| RepeatCount | |
| RepeatDuration | |
| RetainCount | (Inherited from NSObject) |
| Self | (Inherited from NSObject) |
| ShadowColor | |
| ShadowOffset | |
| ShadowOpacity | |
| ShadowPath | |
| ShadowRadius | |
| ShouldRasterize | |
| Speed | |
| Style | |
| Sublayers | |
| SublayerTransform | |
| Superclass | (Inherited from NSObject) |
| SuperHandle |
Handle used to represent the methods in the base class for this NSObject. (Inherited from NSObject) |
| SuperLayer | |
| TimeOffset | |
| ToneMapMode | |
| Transform | |
| Transition |
Represents the value associated with the constant kCATransition |
| VisibleRect | |
| WantsExtendedDynamicRangeContent | |
| WeakDelegate |
An object that can respond to the delegate protocol for this type |
| WeakPreferredDynamicRange | |
| Zone | (Inherited from NSObject) |
| ZPosition | |