PDA

View Full Version : drawInContext not getting called in CALayer subclass




99miles
Dec 17, 2008, 12:11 AM
Hi-
I am trying to subclass CALayer, but for some reason drawInContext is not getting called.

In the init method subclass I have [self setNeedsDisplay] which calls -(void)display if I have it in there, but not
- (void)drawInContext:(CGContextRef)ctx

I am invoking this in the applicationDidFinishLaunching method of my main app file simply like this:

CALayer *root = [theView layer];
TileViewNew *theLayer = [[TileViewNew alloc] init];
[root addSublayer:theLayer];

Any ideas what I'm doing wrong? The docs say drawInContext should get called when setNeedsDisplay is called.

Thanks!



99miles
Dec 17, 2008, 09:58 AM
I guess while I'm here I should confirm this is what I need to do what I'm trying to do.

I'm simply trying to have a CALayer that contains an image with text on it. It appears I can do that in the drawInContext method (if I can get it called).
Does that sounds like what I want to do?

99miles
Dec 17, 2008, 10:45 AM
It looks like what I want is a layer-backed view, but for iPhone UIView doesn't have setWantsLayer. can UIView be set up to be layer-backed?

bnut
Dec 18, 2008, 01:52 AM
It is my understanding that UIView automatically has a layer in it and that that layer automatically has the view as its delegate. In most instances it's not necessary to override CALayer, so you could just use that directly as you have said, then set its delegate to the view.

In the view define displayLayer: to set the contents of the layer as a CGImageRef (cast to id) of the image you want in the subview.

Alternatively if you want to subclass it, you can do the same in the display method of the subclass.

CommanderData
Dec 19, 2008, 12:16 PM
Hi-
I am trying to subclass CALayer, but for some reason drawInContext is not getting called.

In the init method subclass I have [self setNeedsDisplay] which calls -(void)display if I have it in there, but not
- (void)drawInContext:(CGContextRef)ctx

I am invoking this in the applicationDidFinishLaunching method of my main app file simply like this:

CALayer *root = [theView layer];
TileViewNew *theLayer = [[TileViewNew alloc] init];
[root addSublayer:theLayer];

Any ideas what I'm doing wrong? The docs say drawInContext should get called when setNeedsDisplay is called.

Thanks!

If you're not using multiple layers for fancy animation then you just want:

- (void)drawRect:(CGRect)rect

which will be called when [self setNeedsDisplay] is triggered. Then you can get the context of your view and draw all over it to your hearts content. I have done a lot of this.

If you really do want multiple CALayers that you'll paint with:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

Then you have a lot more work ahead of you. Most of the Core Animation samples and discussion on the web are for full-blown Mac OS X, and will not work as-is on the iPhone.

You'll need to:

1) Create CALayers you want to use, add them as sublayers of the UIView's layer.
2) Make sure you have set a delegate for them where your drawLayer resides.
3) To update a layer, you don't call [self setNeedsDisplay], you specify the layer as in [myLayer setNeedsDisplay].

I'm certainly no authority on CALayers, but I've spent the past several days working on a multiple layered view and finally think I understand enough to be dangerous :D