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

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

问题描述

我想在UIView上执行透视变换(如在coverflow中看到的)



不知道这是否可能?



我使用 CALayer 调查了所有实用程序员核心动画播客,



任何帮助,指针或示例代码片段都将非常感谢!


本身说,你需要使用UIView的图层,使用CATransform3D来执行图层的旋转。要获得透视效果的诀窍,如这里所述,是直接访问CATransform3D(m34)的矩阵单元格之一。矩阵数学从来不是我的东西,所以我不能解释为什么这个工作,但它是。您需要将此值设置为初始变换的负分数,然后将层旋转变换应用于此。您还应该能够执行以下操作:

  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;

每次轮换从头开始重建图层转换。



一个完整的例子(有代码)可以找到在这里,我已经实现了基于触摸旋转和缩放在几个CALayers,基于比尔Dudney的例子。最新版本的程序,在页面的最底部,实现这种透视操作。



你在响应中引用的sublayerTransform是一个应用于UIView的子层的转换, CALayer 。如果你没有任何子层,不要担心它。我在我的例子中使用sublayerTransform简单,因为有两个CALayers包含在我旋转的一层。


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

Does anyonew know if this is possible?

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!

解决方案

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:

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;

which rebuilds the layer transform from scratch for each rotation.

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.

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天全站免登陆