CALayer & drawRect

In the "Architecture of views and windows" is indicated, quote:

Views work in conjunction with Core Animation layers to handle rendering and animation of the contents of views. Each view in UIKit is supported by a level object (usually an instance of the CALayer class), which manages the backup storage for presentation and processes the viewing-related animations.

Further in the section "Drawing viewing cycle" is indicated:

The UIView class uses an on-demand drawing model to represent content. When a view first appears, a request for its content appears on the screen. The system captures a snapshot of this content and uses this snapshot as a visual representation of the view.

Does this mean that the content created in the view in the drawRect method call is recorded in the snapshot stored in its base layer of basic support?

If not, where is this snapshot of the content โ€œlocatedโ€?

If this is not the case, does this mean that CALayer is used to render โ€œstaticโ€ content, content that does not change very often, and drawRect is used to render content that often changes, for example, in a game application?

ps

The questions are not related to any particular code implementation.

I just want to understand the architecture of the ios presentation layer.

+6
source share
3 answers

Does this mean that the content created in the view in the drawRect method call is recorded in the snapshot stored in its base layer of basic support?

Yes. Everything uses layers under the hood. UIView -drawRect will capture what you draw and (as far as I know) set content to a sublayer of the view. It can even do this on the base layer object. Where the snapshot is saved.

If not, does this mean that CALayer is used to render โ€œstaticโ€ content, content that does not change very often, and drawRect is used to render content that often changes, for example, in a game application?

How often content changes do not affect the selection. There is not much difference in using drawRect versus manually creating CALayer s. It depends on how you want to arrange sub-elements in your views or if you want to create reusable level objects without UIView details (like CATextLayer ). If you have different different sub-elements, you can split them into different layers using your own drawing code. If you just have one simple content to draw, you can do it in one implementation of drawRect .

Having said that, you need to know that each level will become a separate "GPU" element, so there may be performance benefits for reducing the number of layers you have, or using the shouldRasterize + rasterizationScale property of the parent layer. This takes a snapshot of the entire hierarchy of layers and creates a single rasterized image that will be displayed instead of n individual ones.

+10
source

Does this mean that the content created in the view in the drawRect method call is recorded in the snapshot stored in its base layer of basic support?

Two words: "implementation details"

If this is not the case, does this mean that CALayer is used to render โ€œstaticโ€ content, content that does not change very often, and drawRect is used to render content that often changes, for example, in a game application?

Not really. Layers are very good at animating content (as hinted at by the name of the Core Animation framework). drawRect is good for advanced drawing, but it can slow down the redrawing of each frame (obviously, depending on what you draw).

I have not seen the reference to the Core Animation Programming Guide in your question. This is a good place to learn more about the element layer.

+1
source

Each UIView has a base CALayer (which is actually rendered on the screen).

CALayer is just a bitmap (holds pixels). When you call setNeedsDisplay on your view, CALayer gets a mark for redrawing. At the end of the execution loop, after processing the events, a CGContextRef is created and the drawRect delegate is called. Then you do things in the created context, which is then copied to a bitmap and eventually compiled with other layers displayed on the screen.

So yes, the snapshot is stored in CALayer. This is just an optimization, so layers should not redraw themselves unless they are marked as redrawn (using setNeedsDisplay).

0
source

All Articles