Search code examples
ioscore-animationcore-graphicscalayer

How to draw multiple CALayer sublayers from parent layer


I have a CALayer with two subLayers that contain semi-static content. While rendering the top layer I would like to occasionally just go ahead and render the sublayers without scheduling an update with setNeedsDisplay.

I know that one option is to create delegate classes and implement drawLayer:inContext: but I wanted to render the sublayers immediately perhaps with renderInContext: but I don't want to render into the parent layer.

Is there a way I can switch or obtain the context of the sublayers to do immediate drawing into them?


Solution

  • The mechanism for doing the drawing while holding on to the state in a "root" object is to set the root object as the delegate for any layer for which it will do the rendering. Then you should implement drawLayer:inContext: in the delegate.

    Note that a layer will not require redrawing unless you ask it to with setNeedsDisplay. So in your case, would would loop through your state and call [layer setNeedsDisplay] only those layers that need it and in turn only those layers will call drawLayer:inContext:.