嵌套 CALayers 不接受透视变换
我正在尝试利用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
技巧是使用 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.