What is the context passed to drawLayer: inContext :?

In Iphone development, I want to draw context in a layer. Then the question arises:

What is the context passed to drawLayer: inContext :? Is this a content context context or a UIview context?

If this is a UIView context, which UIView is this?

Thanks in advance.

+4
source share
2 answers

The context passed belongs to CALayer, also returned by this delegate method. This is usually a display context, but it can also be an image or PDF context if the layer is drawn manually using -renderInContext:

CALayers can exist independently or be used as support for UIView. All UIViews have a layer behind them that handles the actual display of the contents of this view. Drawing in the view is actually drawn on its layer, and thus drawing in CALayer, which supports UIView, will be displayed for presentation.

As I said, you can create CALayers that exist as separate objects and add them to existing layers as sublayers for display. At some point, there should be a UIView that places all of these sublayers in its background layer so that these layers appear on the iPhone screen.

Note that according to the UIView class reference :

Since the view is a delegate layer, you should never set to view another CALayer object as a delegate. In addition, you should never change the delegate of this layer.

This means that for the UIView layer, you will process the delegate method in UIView in almost all cases, so the level that was passed to this method will be the view level. Sublayers can have anything as their delegate, because they are not tied to a particular kind.

+3
source

There is information here: Providing Level Content

If you must draw the content of the layers, rather than loading it from an image, you implement the drawLayer: inContext: delegate method. The delegate is given a layer for which content is required, and a CGContextRef to draw the content.

So this is usually the context of your delegate object. In the case of UIVIew, the presentation itself is a delegate.

+1
source

Source: https://habr.com/ru/post/1302467/


All Articles