CGAffineTransformRotate 应用于 UIView 后宽度和高度增加

发布于 2024-12-31 22:08:51 字数 556 浏览 4 评论 0原文

我想以编程方式旋转视图,因为我使用了下面的代码:

CGAffineTransform rotateTransform = contentView.transform;
CGAffineTransform rotatenewTransform  = CGAffineTransformRotate(rotateTransform,aFloatRotate);
contentView.transform = rotatenewTransform;

当我这样做时,contentView(我的 UIView)的宽度和高度会增加。

我应该怎么做才能让 contentView 保持原样并且也旋转?

提前致谢。

在这里我更详细地解释我的问题:

我的内容视图包含一个按钮和一个图像视图。 此 contentView 添加在背景中,并且此 contentView 不应超出此背景边界。

但是当我通过上面的代码旋转这个 contentview 时,contentView 的宽度和高度会增加,所以有时它会超出背景边界。

我希望您能清楚这一点。 :)

I want to rotate a view programmatically for that I have used below code:

CGAffineTransform rotateTransform = contentView.transform;
CGAffineTransform rotatenewTransform  = CGAffineTransformRotate(rotateTransform,aFloatRotate);
contentView.transform = rotatenewTransform;

When I do this the width and height of contentView (my UIView) increase.

What should I do so that contentView remains as it is and it rotates too?

thanks in advance.

Here i am explaining my problem in more details :

my contentview contains a buttons and a imageView in it.
this contentView added in a background and this contentView should not go out of this background boundry.

but when i rotate this contentview by upper code, widht and height of contentView increases and so some times it goes out of background boundry.

I hope this will be clear to you. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2025-01-07 22:08:51

宽度和高度(大概是从 frame.size 中获取的)必须更改,因为它们描述了容纳整个旋转视图的最小矩形 - 如果将矩形旋转 45 度,则该矩形保持旋转后的矩形比原始矩形更宽、更高。

旋转视图的“真实”大小仍将在边界矩形中可用 - 这是在未旋转的视图的内部坐标系中表示的。

因此,如果您的原始 frame 是原点 (100,100)、大小 (100,50),则旋转后的视图将有一个 frame,其中原点和大小是一个矩形,可以将旋转视图放入其中,如超级视图的坐标系中所述。如果您现在这样做:

CGFloat width = contentView.frame.size.width;

您将获得更改后的值。但是,如果您这样做:

CGFloat width = contentView.bounds.size.width;

您将获得原始的 100 宽度值。

The width and height (presumably you are taking this from frame.size) have to change because they describing the smallest rectangle that holds the entire rotated view - if you rotate a rectangle by 45 degrees, then the rectangle to hold the rotated rectangle is wider and taller than the original rectangle.

The "real" size of your rotated view will still be available in the bounds rectangle - this is expressed in the view's internal coordinate system which is not rotated.

So, if your original frame was origin (100,100), size (100,50), your rotated view would have a frame where the origin and size was a rectangle that could fit your rotated view within it, described in the superview's coordinate system. If you now did this:

CGFloat width = contentView.frame.size.width;

You would get your changed value. However, if you did this:

CGFloat width = contentView.bounds.size.width;

You would get your original 100 width value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文