drawInContext not getting called in CALayer subclass

Discussion in 'iOS Programming' started by 99miles, Dec 16, 2008.

  1. 99miles macrumors member

    Oct 10, 2008
    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.

  2. 99miles thread starter macrumors member

    Oct 10, 2008
    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?
  3. 99miles thread starter macrumors member

    Oct 10, 2008
    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?
  4. bnut macrumors newbie


    Nov 16, 2008
    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.
  5. CommanderData macrumors regular

    Dec 1, 2007
    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

Share This Page