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

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

问题描述

我很惊讶没有找到这个问题的答案,也许是我以某种方式忽略了一些非常简单的事情:

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

如何在对 UIView 应用 CGAffineTransform 后获取其实际大小?

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: 到或从视图.

[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]在superview中找到位置.

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

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

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