CALayer delegate methods not called on layer hosted view

徘徊边缘 提交于 2020-07-22 22:20:56

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21078009/calayer-delegate-methods-not-called-on-layer-hosted-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!