tags:

views:

512

answers:

2

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

What exactly is the context passed into drawLayer:inContext:? Is it the layer's contents's context or the UIview's context?

If this is the UIView's context, which UIView it is?

Thanks in advance.

A: 

There is some information here: Providing Layer Content

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

So normally is the context of your delegate object. In the case of UIVIew, the view itself is the delegate.

Yannick L.
A: 

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

CALayers can exist on their own, or be used as the backing for a UIView. All UIViews have a layer behind them, which handles the actual display of that view's content. Drawing in a view actually draws on its layer, and, likewise, drawing in a CALayer that backs a UIView will appear to draw to the view.

As I said, you can create CALayers that exist as separate entities, and add them to existing layers as sublayers for display. At some point, there will need to be a UIView that hosts all of these sublayers within its backing layer in order for these layers to be seen on the iPhone's screen.

Note that according to the UIView class reference:

Since the view is the layer’s delegate, you should never set the view as a delegate of another CALayer object. Additionally, you should never change the delegate of this layer.

This means that for a UIView's layer, you would be handling the delegate method within the UIView in almost all cases, so the layer passed in to that method would be the view's layer. Sublayers can have anything as their delegate, because they are not attached to a particular view.

Brad Larson