我可以在带有子视图的 NSView 上设置 layer.transform 吗?

发布于 2024-11-06 21:46:20 字数 250 浏览 0 评论 0原文

我想要一个带有子视图(图像、文本等)的 NSView,然后使用 myView.layer.transform = myTransform 对它应用 3d 变换。

阅读文档后,我对是否允许这样做感到困惑。如果它是图层支持的视图,那么我不应该直接与图层交互,因此设置变换似乎是错误的。

如果它是层托管视图,那么我不应该有子视图。

如果我想设置layer.transform,这是否意味着我必须通过CALayer设置所有绘图,并停止使用子视图?

I'd like to have an NSView with subviews (images, text, etc.) and then apply a 3d transform to it using myView.layer.transform = myTransform.

After reading the docs I am confused as to whether this is allowed. If it's a layer-backed view, then I am not supposed to be interacting with the layer directly, so setting the transform seems wrong.

If it's a layer-hosting view, then I'm not supposed to have subviews.

If I want to set layer.transform, does that mean I have to set up all my drawing through CALayer, and stop using subviews?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2024-11-13 21:46:20

我刚刚在具有嵌套子视图 NSScrollViews 的 NSView 上设置了 CALayer 动画,并且工作正常。我认为限制在于重叠、叠加的视图。 (后者的问题是没有指定的绘制顺序;无法预测哪个视图将显示在顶部。)

但是如果您也想将变换应用于子视图,则必须分别为它们设置图层。或者,是的,您可以转储子视图并将所有内容放在单个视图的 CALayers 上。您可以使用边界和位置属性来控制它们的大小和位置。 (注意位置是从中心开始,而不是左下角,除非您更改锚点。)

如果您正在进行图层托管,请勿将变换应用于根图层(view.layer)。相反,创建一个新的 CALayer,向其中添加内容,向其中添加变换,然后将其作为子图层应用到根图层。避免直接处理根层。

层托管设置的快速示例:

// Set up the root layer.
[[self.aViewController view] setLayer:[CALayer layer]];
[[self.aViewController view] setWantsLayer:YES];
// Set up a sublayer.
CALayer *sublayer = [CALayer layer];
[self.aViewController.view.layer addSublayer:sublayer];
// Repeat if you need additional sublayers. There's a name property if you need to distinguish between them.

I just set up a CALayer animation on an NSView that has nested subviews, NSScrollViews, and it works OK. I think the restriction is on overlapping, superimposed views. (The problem with the latter is that there is no specified drawing order; it's unpredictable which view will be displayed on top.)

But if you want to apply the transform to the subviews as well, you would have to set up layers for them separately. Or, yes, you could dump the subviews and put everything on the CALayers of a single view. You can control their size and placement using the bounds and position properties. (Note position is from center, not lower left, unless you change the anchor point.)

If you're doing layer-hosting, do not apply your transform to the root layer (view.layer). Instead, make a new CALayer, add contents to it, add the transform to it, and apply it as a sublayer to the root layer. Avoid working with the root layer directly.

Quick sample of layer-hosting setup:

// Set up the root layer.
[[self.aViewController view] setLayer:[CALayer layer]];
[[self.aViewController view] setWantsLayer:YES];
// Set up a sublayer.
CALayer *sublayer = [CALayer layer];
[self.aViewController.view.layer addSublayer:sublayer];
// Repeat if you need additional sublayers. There's a name property if you need to distinguish between them.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文