gpt4 book ai didi

ios - 如何将透视变换应用于 UIView?

转载 作者:IT王子 更新时间:2023-10-29 07:26:06 24 4
gpt4 key购买 nike

我希望对 UIView 执行透视变换(如在 coverflow 中看到的那样)

有人知道这是否可能吗?

我研究过使用 CALayer 并浏览了所有务实的程序员 Core Animation 播客,但我仍然不清楚如何在 iPhone 上创建这种转换。

非常感谢任何帮助、指示或示例代码片段!

最佳答案

正如 Ben 所说,您需要使用 UIView 的 层,使用 CATransform3D 执行层的 旋转。获得透视效果的技巧,如described here , 是直接访问 CATransform3D (m34) 的矩阵 cells 之一。矩阵数学从来都不是我的事,所以我无法确切解释为什么它有效,但它确实有效。您需要将此值设置为初始变换的负分数,然后对其应用图层旋转变换。您还应该能够执行以下操作:

objective-C

UIView *myView = [[self subviews] objectAtIndex:0];
CALayer *layer = myView.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / -500;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;

swift 5.0

if let myView = self.subviews.first {
let layer = myView.layer
var rotationAndPerspectiveTransform = CATransform3DIdentity
rotationAndPerspectiveTransform.m34 = 1.0 / -500
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, 45.0 * .pi / 180.0, 0.0, 1.0, 0.0)
layer.transform = rotationAndPerspectiveTransform
}

它为每次旋转从头开始重建图层转换。

完整的示例(带有代码)可以在here 中找到。 ,我根据 Bill Dudney 的示例在几个 CALayers 上实现了基于触摸的旋转和缩放。最新版本的程序,在页面的最底部,实现了这种透视操作。代码应该相当简单易读。

您在响应中提到的 sublayerTransform 是应用于您的 UIView 的 CALayer 的子层的转换。如果您没有任何子层,请不要担心。我在我的示例中使用 sublayerTransform 只是因为我正在旋转的一层中包含两个 CALayers

关于ios - 如何将透视变换应用于 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/347721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com