What is the relationship between UIView and CALayer

So, here is my understanding: in the UIView and CALayer classes setNeedsDisplay, UIView is a composite class that includes CALayer. But UIView uses drawRect:, while CALayer uses drawLayer:inContext:They use setNeedsDisplayto call. Is this some kind of protocol or something else? So, I assume that at some point in the implementation of the UIView, a CALayer is created and this layer delegate is set to the UIView class? What exactly is the relationship between these two classes? So, drawRect:in UIView basically parses its contents and calls drawLayer:inContext:its layer? What is this design? Thanks, I'm trying to get my head around how these two classes work.

+5
source share
1 answer

These are all the details of Apple's implementation, so I don’t know for sure. In addition, different presentation classes will have different relationships with their layer.

For example, UIImageView explicitly documented so as not to invoke drawRect:. This is probably due to the fact that setting the image actually updates the layer property contentsdirectly, without going through rendering methods.

However, I believe that the main behavior UIViewis that it setNeedsDisplaysimply redirects the call to this level. Then, when the view is invoked drawLayer:inContext:, it adjusts the contents of the context UIGraphicsand invokes drawRect:.

0
source

All Articles