应用CGAffineTransform后获取UIView的大小 [英] Get size of UIView after applying CGAffineTransform

查看:135
本文介绍了应用CGAffineTransform后获取UIView的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶没有找到这个问题的答案,也许这很简单,但是我却忽略了它:

I was surprised not to find an answer to this question, maybe is something very simple I somehow overlook :

在将CGAffineTransform应用于UIView之后,如何获取UIView的实际大小?

How to get the real size of an UIView after I apply a CGAffineTransform to it?

例如

我的UIView的尺寸为300 x 200,我应用了缩放变换,假设水平和垂直因子为2,因此UIView现在在屏幕上需要600 x 400,但是它是边界,并且图层的边界仍返回大小300 x 200 ...在哪里可以找到UIView的实际大小?

my UIView has size 300 x 200, I apply a scaling transform let's say factor 2 both horizontal and vertical, so the UIView now takes 600 x 400 on the screen, but it's bounds and it's layer's bounds are still returning a size of 300 x 200 ... where do I find the real size of the UIView ?

ps.忘了提我也要旋转uiview.如果仅应用缩放CGSizeApplyAffineTransform效果很好,但是如果同时旋转,则无法正常工作.

ps. forgot to mention I want to also rotate the uiview. If I apply only scaling CGSizeApplyAffineTransform works great, but when there's also rotation, then it does not work properly.

drawonward向我指出了正确的方向,我只是对要编译的代码做了一些改进,这里是:

drawnonward pointed me in the right direction, I just refined a bit the code to compile and here it is :

UIView* view = (your view being transformed);
CGAffineTransform trans = (view.transform or create a new transformation);

CGRect rect = [view bounds];
CGMutablePathRef path = CGPathCreateMutable();
rect.origin = CGPointZero;
CGPathAddRect(path , &trans , rect);
rect = CGPathGetBoundingBox( path );
CGPathRelease( path );

现在rect.size包含应用了转换的视图的尺寸 再次感谢drawonward

Now rect.size contains the dimensions of the view with the transformation applied Thanks again to drawnonward

推荐答案

[myView frame]返回父级所见的视图框架,以获取布局和相对大小. [myView bounds]返回视图本身的边界,以进行绘制.如果将变换应用于多个视图,则可以对视图使用convertRect:或从视图使用convertRect:.

[myView frame] returns the frame of the view as seen by the parent, for layout and relative sizes. [myView bounds] returns the bounds of the view as seen by itself, for drawing. If you have transforms applied to multiple views, you can use convertRect: to or from a view.

也许是这样.

CGRect rect = [view bounds];
CGPathRef path = CGPathCreateMutable();
rect.origin = CGPointZero;
CGPathAddRect( rect , [view transform] );
rect = CGPathGetBoundingBox( path );
CGPathRelease( path );

使用[view center]在超级视图中查找位置.

The use [view center] to find the position in the superview.

这篇关于应用CGAffineTransform后获取UIView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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