It is known that the zPosition
of the layers only determines which layer cover up which layer. Whether it is a zPosition
of 10 or 1000 won't affect its position.
That is, unless if we use CATransformLayer
to contain those layers, then the zPosition
of those layers will affect the layers' position.
However, the following code running in iOS 5.1.1 does make the zPosition
alter the position of the layers... you can try it in a new Single View App, and add the following code to ViewController.m
. If the zPosition
of layer2
is changed from 88
to 188
, we can see that the layer moves accordingly. So no CATransformLayer
is in the code; why will it behave like that? (Please quote Apple docs or any reference).
Also related is, if the line self.view.layer.sublayerTransform = transform3D;
is changed to self.view.layer.transform = transform3D;
then the zPosition
will have no effect on the position. But according to the Apple docs, transform
and sublayerTransform
only differ in whether self is transformed or not:
Two layer properties specify transform matrices:
transform
andsublayerTransform
. The matrix specified by thetransform property
is applied to the layer and its sublayers relative to the layer'sanchorPoint
. [...] The matrix specified by thesublayerTransform
property is applied only to the layer’s sublayers, rather than to the layer itself.
So it is strange that why changing that will cause self.view.layer
to act like a CATransformLayer
.
-(void) viewDidAppear:(BOOL)animated {
CATransform3D transform3D = CATransform3DIdentity;
transform3D.m34 = -1.0 / 1000;
transform3D = CATransform3DRotate(transform3D, M_PI / 4, 0, 1, 0);
self.view.layer.sublayerTransform = transform3D;
CALayer *layer1 = [[CALayer alloc] init];
layer1.zPosition = 33;
layer1.frame = CGRectMake(100, 100, 100, 100);
layer1.backgroundColor = [[UIColor orangeColor] CGColor];
[self.view.layer addSublayer:layer1];
CALayer *layer2 = [[CALayer alloc] init];
layer2.zPosition = 88;
layer2.frame = CGRectMake(100, 120, 100, 100);
layer2.backgroundColor = [[UIColor yellowColor] CGColor];
[self.view.layer addSublayer:layer2];
}