On iOS, why does setting a layer's sublayerTransform turn itself to act like CATranformLayer?

3.8k views Asked by At

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 and sublayerTransform. The matrix specified by the transform property is applied to the layer and its sublayers relative to the layer's anchorPoint. [...] The matrix specified by the sublayerTransform 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];    

}
1

There are 1 answers

0
signal On

we can think the layer as a coordinate layer , when we make a layer transfrom3D, it coordinate become 3D,but its subLayer still show as 2D,but when set sublayer transfrom3D,the layer will make its all sublayer show as 3D.it is the same as the rotation