带有自定义 CALayer 的层支持的 NSView 不调用 updateLayer? [英] Layer backed NSView with custom CALayer not calling updateLayer?

查看:23
本文介绍了带有自定义 CALayer 的层支持的 NSView 不调用 updateLayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义层支持的 NSView 并覆盖了 makeBackingLayer 方法以返回自定义 CALayer 子类.我还覆盖了 WantUpdateLayer 以返回 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 返回自定义CALayer` 时,您实际上将自定义层托管在父支持层内.(本人纯属猜测)

I can't quite put my finger on it, but other instances point to the notion that when your makeBackingLayerreturns a customCALayer` 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:

任何见解将不胜感激.

推荐答案

你现在肯定已经解决了,但对于未来的读者,我通过阅读 NSView 文档

Surely you fixed it by now, but for future readers I found a possible explanation by reading the NSView documentation!

如果 canDrawSubviewsIntoLayer 属性设置为 YES,则视图忽略此方法返回的值.相反,视图总是使用它的 drawRect: 方法来绘制它的内容.

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.

这篇关于带有自定义 CALayer 的层支持的 NSView 不调用 updateLayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆