使用自定义CALayer的层支持NSView不调用updateLayer?

Sam*_*Sam 12 cocoa core-animation calayer nsview appkit

我有一个自定义的图层支持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
}
Run Code Online (Sandbox Code Playgroud)

一旦我这样做,我发现当我设置NSView.needsDisplay = true它时,路由调用自定义层的drawInContext:方法而不是updateLayer:方法.为什么这样做?在我的例子中,我检查了如果我删除了makeBackingLayer覆盖,那么我updateLayer的预期方式被调用.

我无法完全理解它,但其他实例指出的是,当你的makeBackingLayer returns a customCALayer`实际上你的自定义层托管在父背衬层内时.(我的纯粹猜测)

此外,鉴于CALayer的drawInContext:更"低级",两条绘图路线之间是否会有不同的性能特征?有关该问题的更多详细信息,请参阅此SO问题:直接在CALayer.drawInContext中呈现的图层支持的NSView性能:

任何见解将不胜感激.

Ast*_*ria 2

您是否忘记设置layerContentsRedrawPolicywantsLayer属性?

self.wantsLayer = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay;
Run Code Online (Sandbox Code Playgroud)