问题描述
我有一个自定义的支持图层的NSView,并且覆盖了makeBackingLayer方法以返回一个自定义的CALayer子类.我还覆盖了wantUpdateUpdateLayer返回true,从而完全选择了类似图层的绘图.
I have a custom layer-backed NSView and have overidden the makeBackingLayer method to return a custom CALayer subclass. I have also overriden wantsUpdateLayer to return true thereby fully opting into layer-like drawing.
override func makeBackingLayer() -> CALayer {
return Layer() // my custom class with "drawLayer:" implemented
}
override var wantsUpdateLayer:Bool{
return true
}
// never called
override func updateLayer() {
super.updateLayer()
println("updateLayer after")
self.layer?.borderWidth += 1
}
执行完此操作后,我发现设置NSView.needsDisplay = true
会将调用路由到自定义层的drawInContext:方法,而不是updateLayer:方法. 为什么这样做?在我的示例中,我检查了是否删除了makeBackingLayer
覆盖,然后按预期方式调用了updateLayer
.
Once I do this, I find that when I set NSView.needsDisplay = true
it routes calls to the custom layer's drawInContext: method as opposed to the updateLayer: method. Why does it do this? In my example, I have checked that if I remove the makeBackingLayer
override then my updateLayer
is called in the expected manner.
我不太愿意指责它,但是其他实例则指出了这样一个概念,即当您的makeBackingLayer returns a custom
CALayer`时,您实际上已将自定义层托管在父级支持层中. (纯粹是我的猜测)
I can't quite put my finger on it, but other instances point to the notion that when your makeBackingLayerreturns a custom
CALayer` that you actually have your custom layer hosted inside a parent backing layer. (Pure Speculation on my part)
此外,考虑到CALayer的drawInContext:更低级",两条绘制路线之间会有不同的性能特征吗?有关此问题的更多详细信息,请参见此SO问题: Layer支持的NSView性能,可直接在CALayer.drawInContext中进行渲染:
Further, would there be different performance characteristics between the two drawing routes given that CALayer's drawInContext: is more "low-level"? See this SO question for more detail on that question: Layer-backed NSView performance with rendering directly in CALayer.drawInContext:
任何见识将不胜感激.
Any insight would be greatly appreciated.
推荐答案
您现在肯定已经解决了,但是对于以后的读者,我通过阅读 NSView文档!
Surely you fixed it by now, but for future readers I found a possible explanation by reading the NSView documentation!
这篇关于带有自定义CALayer的层支持的NSView不调用updateLayer吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!