嵌套 CALayers 不接受透视变换

发布于 2024-08-24 11:45:15 字数 867 浏览 7 评论 0原文

我正在尝试利用 CALayers 在我的一个应用程序中创建 3D 风格效果。然而,嵌套的 CALayers 似乎没有应用透视。

为了用伪代码进行说明,如果我有一个应用了透视的图层,如下所示:

CATransform3D subLayerTransform = CATransform3DIdentity;

/* set perspective */
subLayerTransform.m34 = -0.002;
superLayer.sublayerTransform = subLayerTransform;

添加 然后向其添加一个图层,如下所示:

CALayer *sublayer = [CALayer layer];
sublayer.frame = layerFrame;
sublayer.contents = (id)[image CGImage];
[superLayer addSublayer: sublayer];

透视按预期应用。但是,如果我尝试进一步嵌套层,则

CALayer *sublayer = [CALayer layer];
[superLayer addSublayer: sublayer];

CALayer *nestedlayer = [CALayer layer];
nestedlayer.frame = layerFrame;
nestedlayer.contents = (id)[image CGImage];
[sublayer addSublayer: nestedlayer];

嵌套层在旋转时是平​​坦的。

就好像 superLayer 仅将变换应用于图层层次结构中直接位于其下方的图层。是这样吗?或者我做错了什么?

I'm trying to make use of CALayers to create a 3D style effect in one of my applications. However, it seems that nested CALayers do not have perspective applied to them.

To illustrate with pseudo-code, if I have a layer with perspective applied like so:

CATransform3D subLayerTransform = CATransform3DIdentity;

/* set perspective */
subLayerTransform.m34 = -0.002;
superLayer.sublayerTransform = subLayerTransform;

add then add a layer to it like so:

CALayer *sublayer = [CALayer layer];
sublayer.frame = layerFrame;
sublayer.contents = (id)[image CGImage];
[superLayer addSublayer: sublayer];

The perspective is applied as expected. However, if I were to try and nest layers further

CALayer *sublayer = [CALayer layer];
[superLayer addSublayer: sublayer];

CALayer *nestedlayer = [CALayer layer];
nestedlayer.frame = layerFrame;
nestedlayer.contents = (id)[image CGImage];
[sublayer addSublayer: nestedlayer];

The nested layer is flat when rotated.

It's as if the superLayer only applies the transform to layers that are directly below it in the layer hierarchy. Is this the case? Or am I doing something wrong?

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

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

发布评论

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

评论(1

随梦而飞# 2024-08-31 11:45:15

技巧是使用 CATransformLayer 作为子层。

由于该层实际上并不“显示”任何内容,因此您需要添加传统的 CALayers 作为 CATransformLayer 的子级来完成显示内容的工作。但它所做的是出色地维护对象坐标空间,然后将适当的变换应用于其子层。

the trick is to use a CATransformLayer as the sublayer.

As this layer doesn't actually "display" anything, you will need to add traditional CALayers as children of the CATransformLayer to do the job of displaying content. But what it does do is an excellent job of maintaining an object coordinate space and then applying the appropriate transforms to its child layers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文