变换后CALayer与顶层重叠

发布于 2024-10-17 12:24:21 字数 956 浏览 4 评论 0原文

我有两个 CALayer,每个显示一个图像。视图层次结构中的第一层(左)位于第二层之上。问题是,在我绕 y 轴旋转后,右侧图层突然绘制在本应位于顶部的图层上方。

我尝试使用不同的 zPosition 值。没有什么。我的另一个猜测是,右侧图层的动画可能在左侧图层的动画完成之后完成,因此它绘制在顶部。为了测试这一点,我首先对右侧图层应用了转换,但结果仍然相同。哦,我也尝试过CA交易,但没有成功。

这里有一个屏幕截图:

the Problem

在保持多个 CALayers 的顺序的同时对它们进行动画处理的正确方法是什么?

谢谢,马克。 编辑:

这是转换的代码。我尝试禁用动画,但得到了相同的结果。

[CATransaction begin];

[CATransaction 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];

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:

the problem

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];

[CATransaction 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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你试过在z轴上平移吗?要么让你的顶层有一个更大的数字,要么你的下层有一个更低的数字或负数。首先尝试一些非常大的东西,看看它是否可以解决问题。然后缩小边距,直到图层尽可能接近,而不会导致您看到的变化。像这样的事情:

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

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

[lowerLayer setTransform:trans];

缺点是,如果您计划堆叠一堆层,您可能必须使用这个最小边距并将其乘以您计划显示的层数,从而导致堆栈淡入距离。当然,如果这就是您想要的效果,那将是一件好事。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文