NSView 或 CALayer 的子视图?
今天我决定尝试使用 CALayers 来显示一个覆盖 NSView 的矩形框。该图层将包含一些文本,并将根据何时需要显示可变文本来打开和关闭。我想使用 CALayer 的原因是您可以使用 CALayer 轻松实现漂亮的渲染和动画。我实现了我的图层,它的效果非常好。然而,在使用我的 GUI 并单击几次打开和关闭图层的各种按钮后,我认为我的图层视图的层次结构似乎发生了倾斜。我认为焦点一定已经切换到其他一些再次被关闭的 NSView 。我基本上对在给定时间处理哪一层感到非常困惑,并且失去了对视图层次结构的控制。
我的问题是:我应该使用 NSView 或 CALayers 的子视图来显示应用程序中可能多次出现的内容吗?在我看来,很容易失去对正在处理的层的控制。有没有一种方法可以通过名称来识别当前图层,以便您可以重用该图层,或者最好使用图层,将其删除,然后在下次需要时重新创建图层?
感谢您抽出时间。干杯,特隆德
Today I decided to try to use CALayers to show a rectangular box overlaying a NSView. The layer will contain some text and will be turned on and off depending on when it is necessary to show the variable text. The reason I wanted to use CALayer for this was the nifty rendering and animation you can easily do with CALayer. I implemented my layer and it worked like a charm. However, after using my GUI and clicking several times on various buttons turning the layer on and off, it seemed that the hierarchy of what I thought was my layer view was skewed. I think focus must have been switched to some other NSView which again was turned off. I basically got very confused as to which layer I was handling at a given time and I lost control of the view hierarchy.
My question is: should I use subviews of NSView, or CALayers to show something that may occur many times on and off in an application? It seems to me that it is easy to loose control of which layer you are working on. Is there a way to identify by name the current layer so you can reuse the layer, or is it best to work with layers, delete them and then re-create the layer the next time you need them?
Thanks for your time. Cheers, Trond
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FWIW,
我经常非常快速地“打开和关闭”CALayers,没有任何问题。因此,如果您愿意,您可以这样做!
删除它们并快速重新创建它们似乎不会造成任何问题。 (这不是一个大的资源用户,而且我从未见过这样做的任何其他问题。)因此,如果您愿意,一定要这样做!
我实际上不明白你所说的“命名”层是什么意思 - 当然,作为 iVar,它们有一个名称!你必须命名你所有的CALayers。
这是一段典型的生产代码(来自 .h 文件):
等等。我真的不明白你怎么能“不”说出它们的名字,你知道!
最后,
4,不要忘记图层比 NSViews “更好”得多,因为: NSViews 在重叠的兄弟姐妹之间存在劣质/错误的关系:它们本质上不起作用。在这里阅读相关内容:
将 iOS (iPhone) 应用移植到 Mac?
希望有帮助!
PS - 这些也可能对 CALayers 有帮助...
正是如此CALayer 的 display/drawRect 方法中会发生什么?
CGLayer和CALayer有什么区别和兼容性?
FWIW,
I have often turned CALayers "on and off" very quickly, with no problems at all. So you can do that if you want to!
Deleting them and recreating them quickly on the fly, does not seem to cause any problems. (It's not a big resource user, and I've never seen any other problems doing that.) So definitely do that if you want to!
I actually don't understand what you mean about "naming" layers - of course, as iVars they have a name! You have to name all your CALayers.
Here's a typical bit of production code (from a .h file):
And so on and on. I don't really see how you can "not" name them, you know!
Finally,
4, Don't forget layers are much "better" than NSViews, because: NSViews have shoddy/buggy relationship between overlapping siblings: they essentially don't work. Read about that here:
port an iOS (iPhone) app to mac?
Hope it helps!
PS - these may also help with CALayers...
Exactly what should happen in a CALayer's display/drawRect methods?
What's the difference and compatibility of CGLayer and CALayer?