如何将透视变换应用于 UIView? [英] How do I apply a perspective transform to a UIView?

查看:30
本文介绍了如何将透视变换应用于 UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking to perform a perspective transform on a UIView (such as seen in coverflow)

有谁知道这是否可能?

我已经使用 CALayer 进行了调查,并浏览了所有实用的程序员 Core Animation 播客,但我仍然不清楚如何在 iPhone 上创建这种转换.

I've investigated using CALayer and have run through all the pragmatic programmer Core Animation podcasts, but I'm still no clearer on how to create this kind of transform on an iPhone.

任何帮助、指针或示例代码片段将不胜感激!

Any help, pointers or example code snippets would be really appreciated!

推荐答案

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

As Ben said, you'll need to work with the UIView's layer, using a CATransform3D to perform the layer's rotation. The trick to get perspective working, as described here, is to directly access one of the matrix cells of the CATransform3D (m34). Matrix math has never been my thing, so I can't explain exactly why this works, but it does. You'll need to set this value to a negative fraction for your initial transform, then apply your layer rotation transforms to that. You should also be able to do the following:

目标 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
}

它为每次旋转从头开始重建层变换.

which rebuilds the layer transform from scratch for each rotation.

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

A full example of this (with code) can be found here, where I've implemented touch-based rotation and scaling on a couple of CALayers, based on an example by Bill Dudney. The newest version of the program, at the very bottom of the page, implements this kind of perspective operation. The code should be reasonably simple to read.

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

The sublayerTransform you refer to in your response is a transform that is applied to the sublayers of your UIView's CALayer. If you don't have any sublayers, don't worry about it. I use the sublayerTransform in my example simply because there are two CALayers contained within the one layer that I'm rotating.

这篇关于如何将透视变换应用于 UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆