Upravit

Sdílet prostřednictvím


CALayer Class

Definition

Layers hold the images that are rendered into the screen.

[Foundation.Register("CALayer", true)]
public class CALayer : Foundation.NSObject, CoreAnimation.ICAMediaTiming, Foundation.INSSecureCoding, IDisposable
type CALayer = class
    inherit NSObject
    interface ICAMediaTiming
    interface INativeObject
    interface IDisposable
    interface INSCoding
    interface INSSecureCoding
Inheritance
CALayer
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, T:CoreAnimation.CAEAGLLayer/CAOpenGLLayer, CAReplicatorLayer, CAScrollLayer, CAShapeLayer, CATextLayer, CATiledLayer, CATransformLayer and T:CoreAnimation.QCCompositionLayer.

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 (SizeF.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 (SizeF.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;
	}
}

On macOS, CALayers are optional. To enable them, you must set the P:AppKit.NSView.WantsLayer property to true. You can change the layer for an NSView by setting the P:AppKit.NSView.Layer property.

On macOS, to change the default layer class used for a given NSView, you can override the M:AppKit.MakeBackingLayer* method.

Constructors

CALayer()

Default constructor that initializes a new instance of this class with no parameters.

CALayer(CALayer)

This method must be implemented by derived classes to make a copy of the original layer.

CALayer(IntPtr)

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

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
AutoresizinMask
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 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
ContentsRect
ContentsScale

The scale factor applied to the layer.

CornerRadius

The radius used when drawing rounded corners.

DebugDescription

A developer-meaningful description of this object.

(Inherited from NSObject)
Delegate

An instance of the CoreAnimation.ICALayerDelegate model class which acts as the class delegate.

Description

Description of the object, the Objective-C version of ToString.

(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

The animation duration of this layer, in seconds.

EdgeAntialiasingMask
FillMode

The fill mode for this layer, as defined by the values in CAFillMode.

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.

PresentationLayer
RasterizationScale
RepeatCount
RepeatDuration
RetainCount

Returns the current Objective-C retain count for the object.

(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
Transform
Transition

Represents the value associated with the constant kCATransition

VisibleRect
WeakDelegate

An object that can respond to the delegate protocol for this type

Zone (Inherited from NSObject)
ZPosition

Methods

ActionForKey(String)

Returns the Actions value associated with the specified key.

AddAnimation(CAAnimation, String)

Adds the animation to the render tree for the layer and associates it in Actions with the key key.

AddConstraint(CAConstraint)
AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using NSString keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

(Inherited from NSObject)
AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using string keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

(Inherited from NSObject)
AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddSublayer(CALayer)

Appends the layer to this layer's Sublayers.

AnimationForKey(String)

Returns the animation associated with the key.

AwakeFromNib()

Called after the object has been loaded from the nib file. Overriders must call base.AwakeFromNib().

(Inherited from NSObject)
BeginInvokeOnMainThread(Action) (Inherited from NSObject)
BeginInvokeOnMainThread(Selector, NSObject)

Invokes asynchrously the specified code on the main UI thread.

(Inherited from NSObject)
Bind(NSString, NSObject, String, NSDictionary) (Inherited from NSObject)
Bind(String, NSObject, String, NSDictionary)
Obsolete.
(Inherited from NSObject)
BindingInfo(String)
Obsolete.
(Inherited from NSObject)
BindingOptionDescriptions(String)
Obsolete.
(Inherited from NSObject)
BindingValueClass(String)
Obsolete.
(Inherited from NSObject)
Clone(CALayer)

This method should be overwritten to provide cloning capabilities for the layer.

CommitEditing() (Inherited from NSObject)
CommitEditing(NSObject, Selector, IntPtr) (Inherited from NSObject)
ConformsToProtocol(IntPtr)

Invoked to determine if this object implements the specified protocol.

(Inherited from NSObject)
Contains(CGPoint)
ConvertPointFromLayer(CGPoint, CALayer)
ConvertPointToLayer(CGPoint, CALayer)
ConvertRectFromLayer(CGRect, CALayer)
ConvertRectToLayer(CGRect, CALayer)
ConvertTimeFromLayer(Double, CALayer)
ConvertTimeToLayer(Double, CALayer)
Copy()

Performs a copy of the underlying Objective-C object.

(Inherited from NSObject)
Create()

Factory method to create a new CALayer.

DangerousAutorelease() (Inherited from NSObject)
DangerousRelease() (Inherited from NSObject)
DangerousRetain() (Inherited from NSObject)
DefaultActionForKey(String)
DefaultValue(String)
DidChange(NSKeyValueChange, NSIndexSet, NSString)

Indicates a change occurred to the indexes for a to-many relationship.

(Inherited from NSObject)
DidChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
DidChangeValue(String)

Indicates that a change occurred on the specified key.

(Inherited from NSObject)
Display()
DisplayIfNeeded()
Dispose()

Releases the resources used by the NSObject object.

(Inherited from NSObject)
Dispose(Boolean)

Releases the resources used by the CALayer object.

DoesNotRecognizeSelector(Selector)

Indicates that this object does not recognize the specified selector.

(Inherited from NSObject)
DrawInContext(CGContext)

Draws the layer on the specified context.

EncodeTo(NSCoder)

Encodes the state of the object on the provided encoder

Equals(NSObject) (Inherited from NSObject)
Equals(Object) (Inherited from NSObject)
ExposedBindings() (Inherited from NSObject)
GetBindingInfo(NSString) (Inherited from NSObject)
GetBindingOptionDescriptions(NSString) (Inherited from NSObject)
GetBindingValueClass(NSString) (Inherited from NSObject)
GetContentsAs<T>()
GetDictionaryOfValuesFromKeys(NSString[])

Retrieves the values of the specified keys.

(Inherited from NSObject)
GetHashCode()

Generates a hash code for the current instance.

(Inherited from NSObject)
GetMethodForSelector(Selector) (Inherited from NSObject)
GetNativeField(String)
Obsolete.
(Inherited from NSObject)
GetNativeHash() (Inherited from NSObject)
HitTest(CGPoint)

The furthest descendant in this layer's hierarchy that contains the point p.

Init() (Inherited from NSObject)
InitializeHandle(IntPtr) (Inherited from NSObject)
InitializeHandle(IntPtr, String) (Inherited from NSObject)
InsertSublayer(CALayer, Int32)

Inserts the specified layer into the Sublayers array at the specified index.

InsertSublayerAbove(CALayer, CALayer)

Inserts the specified layer into the Sublayers array immediately after sibling.

InsertSublayerBelow(CALayer, CALayer)

Inserts the specified layer into the Sublayers array immediately prior to sibling.

Invoke(Action, Double) (Inherited from NSObject)
Invoke(Action, TimeSpan) (Inherited from NSObject)
InvokeOnMainThread(Action) (Inherited from NSObject)
InvokeOnMainThread(Selector, NSObject)

Invokes synchrously the specified code on the main UI thread.

(Inherited from NSObject)
IsEqual(NSObject) (Inherited from NSObject)
IsKindOfClass(Class) (Inherited from NSObject)
IsMemberOfClass(Class) (Inherited from NSObject)
LayoutIfNeeded()
LayoutSublayers()
MarkDirty()

Promotes a regular peer object (IsDirectBinding is true) into a toggleref object.

(Inherited from NSObject)
MutableCopy()

Creates a mutable copy of the specified NSObject.

(Inherited from NSObject)
NeedsDisplayForKey(String)
NeedsLayout()
ObjectDidEndEditing(NSObject) (Inherited from NSObject)
ObserveValue(NSString, NSObject, NSDictionary, IntPtr)

Indicates that the value at the specified keyPath relative to this object has changed.

(Inherited from NSObject)
PerformSelector(Selector) (Inherited from NSObject)
PerformSelector(Selector, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double)

Invokes the selector on the current instance and if the obj is not null, it passes this as its single parameter.

(Inherited from NSObject)
PerformSelector(Selector, NSObject, Double, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSObject, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean, NSString[]) (Inherited from NSObject)
PreferredFrameSize()

The preferred size for this layer, in the coordinate of its SuperLayer.

PrepareForInterfaceBuilder() (Inherited from NSObject)
RemoveAllAnimations()

Removes all animations currently attached to the layer.

RemoveAnimation(String)

Removes the specified animation from the layer.

RemoveFromSuperLayer()

Removes this from its SuperLayer.

RemoveObserver(NSObject, NSString)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath.

(Inherited from NSObject)
RemoveObserver(NSObject, NSString, IntPtr)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath and context.

(Inherited from NSObject)
RemoveObserver(NSObject, String)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath.

(Inherited from NSObject)
RemoveObserver(NSObject, String, IntPtr)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath and context.

(Inherited from NSObject)
RenderInContext(CGContext)

Renders the layer into the specified CGContext.

ReplaceSublayer(CALayer, CALayer)
Resize(CGSize)
ResizeSublayers(CGSize)
RespondsToSelector(Selector)

Whether this object recognizes the specified selector.

(Inherited from NSObject)
ScrollPoint(CGPoint)
ScrollRectToVisible(CGRect)
SetContents(NSObject)
SetNativeField(String, NSObject)
Obsolete.
(Inherited from NSObject)
SetNeedsDisplay()
SetNeedsDisplayInRect(CGRect)
SetNeedsLayout()
SetNilValueForKey(NSString)

Sets the value of the specified key to null.

(Inherited from NSObject)
SetValueForKey(NSObject, NSString)

Sets the value of the property specified by the key to the specified value.

(Inherited from NSObject)
SetValueForKeyPath(IntPtr, NSString)

A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

(Inherited from NSObject)
SetValueForKeyPath(NSObject, NSString)

Sets the value of a property that can be reached using a keypath.

(Inherited from NSObject)
SetValueForUndefinedKey(NSObject, NSString)

Indicates an attempt to write a value to an undefined key. If not overridden, raises an NSUndefinedKeyException.

(Inherited from NSObject)
SetValuesForKeysWithDictionary(NSDictionary)

Sets the values of this NSObject to those in the specified dictionary.

(Inherited from NSObject)
ToString()

Returns a string representation of the value of the current instance.

(Inherited from NSObject)
Unbind(NSString) (Inherited from NSObject)
Unbind(String)
Obsolete.
(Inherited from NSObject)
ValueForKey(NSString)

Returns the value of the property associated with the specified key.

(Inherited from NSObject)
ValueForKeyPath(NSString)

Returns the value of a property that can be reached using a keypath.

(Inherited from NSObject)
ValueForUndefinedKey(NSString)

Indicates an attempt to read a value of an undefined key. If not overridden, raises an NSUndefinedKeyException.

(Inherited from NSObject)
WillChange(NSKeyValueChange, NSIndexSet, NSString)

Indicates that the values of the specified indices in the specified key are about to change.

(Inherited from NSObject)
WillChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
WillChangeValue(String)

Indicates that the value of the specified key is about to change.

(Inherited from NSObject)

Extension Methods

ObjectDidBeginEditing(NSObject, INSEditor)
ObjectDidEndEditing(NSObject, INSEditor)
GetValidModes(NSObject, NSFontPanel)
ValidateToolbarItem(NSObject, NSToolbarItem)
GetDebugDescription(INSObjectProtocol)
AcceptsPreviewPanelControl(NSObject, QLPreviewPanel)
BeginPreviewPanelControl(NSObject, QLPreviewPanel)
EndPreviewPanelControl(NSObject, QLPreviewPanel)
GetAccessibilityCustomRotors(NSObject)

Gets the array of UIAccessibilityCustomRotor objects appropriate for this object.

SetAccessibilityCustomRotors(NSObject, UIAccessibilityCustomRotor[])

Sets the array of UIAccessibilityCustomRotor objects appropriate for this object.

Applies to

See also