Where to call setNeedsDisplay to update the CALayer?

I have a view controller ( MagmaViewController

) that controls a view called MagmaView

. For the direct support layer, MagmaView

I assigned my own CALayer subclass called MagmaLayer

by putting the following in the implementation section MagmaView

:

+ (Class)layerClass
{
    return [MagmaLayer class];
}

      

I have a custom drawing that I want MagmaLayer

to draw for MagmaView

, so I put the drawing code in a method -(void) drawInContext:(CGContextRef) context

MagmaLayer

. The problem is that when I do this and run the simulator, the MagmaView looks empty. I set a breakpoint in a method MagmaLayer

-(void) drawInContext:(CGContextRef) context

and it seems that this method is not being called at all.

Obviously I am confused about the need and placement of the command setNeedsDisplay

to start drawing this CALayer. First, I thought that, at least for the first initial appearance of CALayer, issuing a command setNeedsDisplay

is unnecessary if the CALayer is the direct backing layer for the UIView and that any UIView will ensure that its forward backing layer is correctly drawn when needed. and that only the indirect CALayer support layers require special attention to make sure they are drawn. Isn't that so? Also, where should I place the command setNeedsDisplay

to make this direct reference layer a MagmaLayer

target in itself? Should the command setNeedsDisplay

be in the initialization method MagmaView

, which is the view where theMagmaLayer

here? Or should I go further down the chain and put the command setNeedsDisplay

in MagmaViewController

, a view controller that controls MagmaView

, for example, in a controller method viewWillAppear

?

+3


source to share


1 answer


When drawing, you need to specify your own layer. The main implementation will not call -(void) drawInContext:(CGContextRef) context

automatically. Basic setup is to install needsDisplayOnBoundsChange = YES

in init method. If you have custom properties that need to cause redrawing, you will have to implement / extend + (BOOL)needsDisplayForKey:(NSString *)key

.



+2


source







All Articles