So here is my understanding: there is a setNeedsDisplay
in both UIView and CALayer class, UIView is a composite class that includes a CALayer. But UIView uses drawRect:
while CALayer uses drawLayer:inContext:
They both use setNeedsDisplay
to call it. Is this some protocol or something? So I guess at some point in the implementation of UIView, a CALayer is created and this layer's delegat开发者_开发百科e is set to the UIView class? What's exactly the relationship between this two classes? So the drawRect:
in UIView is basically parsing it's content and call drawLayer:inContext:
of its layer? What kind of design pattern is this? I'm trying to wrap my head around how these two classes work, thanks.
This is all Apple's implementation detail so I don't know for sure. Also, different view classes will have different relationships with their layer.
For example, UIImageView
is explicitly documented to not call drawRect:
. This is probably because setting an image actually updates the layer's contents
property directly without going through the rendering methods.
That said, I imagine most basic UIView
behavior is that setNeedsDisplay
simply forwards the call to the layer. Then, when the drawLayer:inContext:
is called on the view, it sets up the UIGraphics
context stuff and calls drawRect:
.
精彩评论