gpt4 book ai didi

cocoa - 带有自定义 CALayer 的图层支持 NSView 不调用 updateLayer?

转载 作者:行者123 更新时间:2023-12-03 16:03:17 24 4
gpt4 key购买 nike

我有一个自定义的层支持 NSView,并覆盖了 makeBackingLayer 方法以返回自定义的 CALayer 子类。我还重写了wantUpdateLayer以返回true,从而完全选择类似图层的绘图。

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 就会以预期的方式调用。

我不能完全指出它,但其他实例指出了这样一个概念:当您的 makeBackingLayer 返回自定义CALayer` 时,您实际上将自定义层托管在父支持层中。 (纯粹是我的猜测)

此外,考虑到 CALayer 的 drawInContext: 更“低级”,两个绘制路由之间是否会有不同的性能特征?有关该问题的更多详细信息,请参阅此问题:Layer-backed NSView performance with rendering directly in CALayer.drawInContext:

任何见解将不胜感激。

最佳答案

当然,你现在已经修复了它,但对于 future 的读者,我通过阅读 NSView documentation 找到了可能的解释。 !

If the canDrawSubviewsIntoLayer property is set to YES, the view ignores the value returned by this method. Instead, the view always uses its drawRect: method to draw its content.

关于cocoa - 带有自定义 CALayer 的图层支持 NSView 不调用 updateLayer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com