如何在UIView上使用CATransform3D添加特定类型的透视图? [英] How do I use CATransform3D on a UIView to add a specific type of perspective?

查看:210
本文介绍了如何在UIView上使用CATransform3D添加特定类型的透视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建iPhone应用程序了一段时间,使用基本的转换(旋转,缩放等),但现在我想做一些更复杂一点。


$ b $数学真的不是我最强的点...但我想知道如何添加透视到UIView(见下图)。我在Photoshop中快速嘲笑了使用倾斜选项的屏幕截图



我已经看了一下stackoverflow的解决方案,我发现我如何应用透视变换到UIView?,它工作得很好 - 但它不是我真正的后,因为最左边的高度大于最右边



有没有人知道我怎么做这个CATransform3D但没有这些不同的高度?



解决方案

如果你只是想倾斜,你不需要3D变换。仿射变换就足够了。

   - (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext );
CGAffineTransform transform = CGAffineTransformIdentity;
transform.b = -0.1;
transform.a = 0.9;
CGContextConcatCTM(ctx,transform);
//在上下文上绘图
}

这是一个修改的副本;从具有类似变换的项目粘贴,但是您可能需要调整参数a和b。这将从左向右( 0.1 / 0.9 )从9升高到1,同时从左到右至90%( 0.9 )。


I've been creating iPhone apps for a while now, using basic transformations (rotations, scale, etc) but now I'd like to do something a little more complex.

Maths really isn't my strongest point... but I was wondering how I might go about adding 'perspective' to a UIView (see the image below). I quickly mocked the screenshot up using skew options in Photoshop.

I have had a look around stackoverflow for solutions to this, I found How do I apply a perspective transform to a UIView? which works excellently - but it's not really what i'm after because the height of the left most edge is larger than the right most edge.

Does anyone know how I might go about doing this CATransform3D but without these differing heights?

解决方案

If you just want to skew, you don't need 3D transform. An affine transform will suffice.

-(void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform.b = -0.1;
    transform.a = 0.9;
    CGContextConcatCTM(ctx,transform);
    // do drawing on the context
}

this is a modified copy&paste from a project which has a similar transform, but you may need to tune the parameters a and b. This will give a 1 in 9 rise from left to right (0.1/0.9), while condensing from left to right to 90% (0.9).

这篇关于如何在UIView上使用CATransform3D添加特定类型的透视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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