Search code examples
objective-cmacoscocoacore-animation

CALayer delegate methods not called on layer hosted view


I have this layer hosted view, which is initialised like so:

// Init layers
self.wantsLayer = YES;
_hostedLayer = [CALayer layer];
_hostedLayer.delegate = self;
self.layer = _hostedLayer;

Weirdly, the delegate method updateLayer is not called.
When I comment out the last 3 lines, it does get called.

What's wrong here?

Hint: Yes I have overridden wantsUpdateLayer and return YES.


Solution

  • When a view asks for a layer it gets a special, private subclass of CALayer by default, which has extra capabilities. Although I haven’t done this since 10.7, in those days it was an all-or-nothing proposition—you either used the default (private) layer the view got, and got to draw using AppKit conventions, OR you made your own CALayer and drawing was all handled by the CALayer itself or by the delegate methods:

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

    So, I’d guess in your case these latter two methods would be called on your view, but not the view-specific -updateLayer.