Search code examples
objective-cioscocoa-touchipadcore-animation

CALayer -display method not being called


I'm building a picture book for the iPad with CoreAnimation. I did something like a Sprite class (a CALayer subclass) in order to be able to load my animation frames from a sprite sheet. Something similar than here: http://mysterycoconut.com/blog/2011/01/cag1/. I'm not using the layer's delegate in order to set the bounds and contentsRect (and in my case, also the position), but I'm doing it directly in the -display method of the CALayer subclass.

I created a dummy and it was working perfectly. When I moved the code into my project, the -display method of my subclass doesn't get called anymore. I have read the documentation of the CALayer and the CoreAnimation Programming Guide tons of times and I can't find any information about when the -display method is being called. I only know that it uses the Template Pattern, that means, I shouldn't call it myself but instead [layer setNeedsDisplay] should be called, which calls the -display method itself. I tried doing this as well, without success.

If you need to see some code, let me know.

Any ideas?

UPDATE

I found the reason why the method wasn't being called. It's so embarrassing that it's not worth mentioning :0. Anyway, I would be still interested to know when exactly the -display method is being called. I noticed that I have to create an animation in order for it to be triggered. If I just added the layer to the display hierarchy with [layer addSublayer:myCustomLayer], the method didn't get called.


Solution

  • Here's what I was doing wrong. I was creating a new layer pointer instead of using the one that was being added to the layer hierarchy.

    CALayer *layer = nil;
    
    if (animatedLayer == YES)
    {
        // Here's the problem, I created a new layer pointer instead of using the one before. 
        AnimatedLayer *layer = [[AnimatedLayer alloc] init];
    
        // more stuff being done to the AnimatedLayer
    }
    else
    {
        layer = [[CALayer alloc] init];
    
        // more stuff being done to the CALayer
    }
    
    [masterLayer addSublayer:layer];
    [layer release];
    

    Such a silly mistake, It took me hours to debug! I should sleep a little more and stop drinking so much coffee.