开发者

CALayer overlaps top layer after transformation

I have two CALayers each showing an image. The first (left) layer in the view hierarchy is on top of the second one. The problem is that after I apply a rotation around the y-axis, the right layer is all of a sudden drawn above the layer that's supposed to be on top.

I tried using different zPosition values. Nothing. My other guess was that maybe the animation of the right layer finishes after the animation for the left layer and hence it is drawn on top. To test this, I applied the transformation for the right layer first, but still the same result. Oh, I also tried CA transactions without any luck.

Here a screenshot:

CALayer overlaps top layer after transformation

What is the correct way to animate a number of CALayers while keeping their order?

Thanks, Mark. EDIT:

Here is the code for the transformation. I tried disabling the animation, but got the same result.

[CATransaction begin];

[CATransac开发者_如何学运维tion setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];

CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / 400;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, DegreesToRadians(70), 0, 1, 0);

 for(CALayer *layer in [self.layer sublayers]) {
    // transform the two layers
    layer.transform = rotationAndPerspectiveTransform;

}
[CATransaction commit];


Have you tried translating on the z axis? Either make your top layer have a larger number or your lower layer have a lower or negative number. Try something really large to start with and see if it addresses the issue. Then narrow the margin until you get the layer as close as possible without causing the shift you're seeing. Something like this:

CATransform3D trans = CATransform3DIdentity;
trans.m34 = 1 / -900;

trans = CATransform3DTranslate(trans, 0.0f, 0.0f, -500.0f);

[lowerLayer setTransform:trans];

The down side is if you're planning to stack up a bunch of layers, you would probably have to use this minimum margin and multiply that by the number of layers you're planning to display causing the stack to fade into the distance. Of course, if that's the effect you're going for, this would be a good thing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜