Nested CALayers not picking up perspective transform
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.c开发者_运维问答ontents = (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?
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.
精彩评论