gpt4 book ai didi

cocoa - 调用setNeedsDisplay :YES on layer-hosting view does not redraw the view

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

我在自定义 NSView 子类中设置了这样的层托管 View :

[self setLayer:rootLayer];
[self setWantsLayer:YES];

在每个子图层上调用 setNeedsDisplay 后,我将所有子图层添加到图层树中。每个图层的内容均由图层委托(delegate)的 drawLayer:inContext 方法提供。

这是我的问题:

初始化 View 后, View 将正确绘制。但是,当模型发生更改并且我从 View Controller 调用 [myCustomView setNeedsDisplay:YES]; 时,不会调用 drawLayer:inContext

我现在很困惑如何更新 View :

  • 我是否必须在图层树中的每个 CALayer 上调用 setNeedsDisplay 方法?
  • 在图层托管 View 本身上调用 setNeedsDisplay:YES 不应该触发整个图层树的重绘吗?

感谢您的帮助。

编辑

我在 NSView 类引用中找到了一些东西

A layer-backed view is a view that is backed by a Core Animation layer. Any drawing done by the view is the cached in the backing layer. You configured a layer-backed view by simply invoking setWantsLayer: with a value of YES. The view class will automatically create the a backing layer for you, and you use the view class’s drawing mechanisms. When using layer-backed views you should never interact directly with the layer.

A layer-hosting view is a view that contains a Core Animation layer that you intend to manipulate directly. You create a layer-hosting view by instantiating an instance of a Core Animation layer class and setting that layer using the view’s setLayer: method. After doing so, you then invoke setWantsLayer: with a value of YES. When using a layer-hosting view you should not rely on the view for drawing, nor should you add subviews to the layer-hosting view.

link to documentation

就我而言,我有一个图层托管 View 。那么这确实意味着我必须手动触发重绘吗?我是否应该在自定义 NSView 中实现伪 drawRect 方法,以在更改的 CALayers 上调用适当的 setNeedsDisplay ?

最佳答案

经过对 Apple 的进一步研究 sample code我发现,如果您使用图层托管 View ,则必须自行处理由于模型更改而必需的屏幕更新。在 NSView 上调用 setNeedsDisplay:YES 不会执行任何操作。

因此,如果必须更新 View ,我们应该编写一个类似 reloadData 的方法,并在其中对每个需要刷新的 CALayer 调用 setNeedsDisplay。我仍然不确定根层上对此方法的调用是否会传播到所有子层,但我不这么认为。

我现在通过在需要重新缓存的各个 CALayer 上调用 setNeedsDisplay 解决了这个问题。它可以正常工作。

关于cocoa - 调用setNeedsDisplay :YES on layer-hosting view does not redraw the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3647914/

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