How do I use CATransform3D on a UIView to add a specific type of perspective?

走远了吗. 提交于 2019-12-04 12:12:01

问题


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?

alt text http://img594.imageshack.us/img594/4354/perspectivel.png


回答1:


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).



来源:https://stackoverflow.com/questions/3275982/how-do-i-use-catransform3d-on-a-uiview-to-add-a-specific-type-of-perspective

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!