iOS Developer Library — Prerelease

Developer

Core Animation Reference Collection CALayerDelegate Informal Protocol Reference

Options
Deployment Target:

On This Page
Language:

CALayerDelegate

The CALayerDelegate informal protocol is implemented by the layer’s delegate to respond to layer-related events. You can implement the methods of this protocol to provide the layer’s content, handle the layout of sublayers, and provide custom animation actions to perform. The object that implements this protocol must be assigned to the delegate property of the layer object.

  • Asks the delegate to update the layer’s contents.

    Declaration

    Swift

    func displayLayer(_ layer: CALayer)

    Objective-C

    - (void)displayLayer:(CALayer * _Nonnull)layer

    Parameters

    layer

    The layer whose contents need updating.

    Discussion

    The layer calls this method early in its update cycle to give your delegate a chance to set the layer’s contents property directly. If you do not implement this method, the layer calls the drawLayer:inContext: method instead.

    Availability

    Available in iOS 2.0 and later.

  • Asks the delegate to draw the layer’s contents.

    Declaration

    Swift

    func drawLayer(_ layer: CALayer, inContext ctx: CGContext)

    Objective-C

    - (void)drawLayer:(CALayer * _Nonnull)layer inContext:(CGContextRef _Nonnull)ctx

    Parameters

    layer

    The layer whose contents need to be drawn.

    ctx

    The graphics context to use for drawing. The graphics context incorporates the appropriate scale factor for drawing to the target screen.

    Discussion

    If you do not want to set the contents of the layer directly using the displayLayer: method, you can implement this method and use it to draw the contents of your layer. You might prefer this method in cases where the layer’s content is already dynamic and must be regenerated.

    The context may be clipped to protect valid layer content. Subclasses that wish to find the actual region to draw can call the CGContextGetClipBoundingBox function.

    Availability

    Available in iOS 2.0 and later.

  • Asks the delegate to lay out the sublayers of the specified layer.

    Declaration

    Swift

    func layoutSublayersOfLayer(_ layer: CALayer)

    Objective-C

    - (void)layoutSublayersOfLayer:(CALayer * _Nonnull)layer

    Parameters

    layer

    The layer that requires layout of its sublayers.

    Discussion

    During a layout update, the layer calls this method to give your delegate a chance to adjust the position or size of any sublayers. Your delegate object is responsible for changing the frame of each sublayer that requires layout.

    If you do not implement this method, the layer calls the layoutSublayersOfLayer: method on the object in the layer’s layoutManager property. If you do implement this method, the layer does not ask its layout manager object to update the layout.

    Availability

    Available in iOS 3.0 and later.

  • Asks the delegate for the specified action.

    Declaration

    Swift

    func actionForLayer(_ layer: CALayer, forKey event: String) -> CAAction?

    Objective-C

    - (id<CAAction> _Nullable)actionForLayer:(CALayer * _Nonnull)layer forKey:(NSString * _Nonnull)key

    Parameters

    layer

    The layer that is the target of the action.

    key

    The identifier of the action.

    Return Value

    An object implementing the CAAction protocol or nil if the delegate does not specify a behavior for the specified key.

    Discussion

    Your delegate object can implement this method when it wants to provide custom actions for a layer. For information about how layer-related actions are chosen, see the actionForKey: method of CALayer.

    Availability

    Available in iOS 2.0 and later.