如何在旋转仿射变换中消除这些幻数?

发布于 2024-11-02 07:22:52 字数 452 浏览 1 评论 0原文

我有一个 320x460(标准 iPhone 屏幕)的视图,我想将其绘制为横向模式,尽管手机不是这样定位的。这看起来是一个简单的任务,我尝试通过创建一个大小为 460x320 的子视图,然后通过设置视图变换将其旋转 90 度来解决。这很有效,只是旋转的视图没有正确居中。为了“修复”这个问题,我添加了一个平移转换,最终看起来像这样:

CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI / 2.0);
[landscapeView setTransform: CGAffineTransformTranslate(rotate, 70.0, 70.0)];

我不介意进行一些调整转换,但我不知道神奇数字 70 是从哪里来的。我只是玩弄它直到边缘正确匹配。我怎样才能摆脱它们,要么通过消除平移变换,要么通过与高度和宽度相关的一些有意义的数学推导出数字?

I have a view that is 320x460 (standard iPhone screen) that I want to draw as if it were in landscape mode, though the phone is not oriented that way. This seems like a simple task, which I tried to solve by creating a subview of size 460x320, and then rotating it 90 degrees through setting the view transformation. This worked, except that the rotated view was not centered correctly. To 'fix' this I added a translation transformation, which ended up looking like this:

CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI / 2.0);
[landscapeView setTransform: CGAffineTransformTranslate(rotate, 70.0, 70.0)];

I don't mind having some adjustment transformation, but I have no clue where the magic number 70 came in. I just played around with it until the edges matched up correctly. How can I get rid of them, either by eliminating the translation transformation, or by deriving the number from some meaningful math related to the height and width?

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

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

发布评论

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

评论(2

墨落成白 2024-11-09 07:22:52

只是一种预感,但我猜测在转换之前您将 LandscapeView 的中心设置(或默认)为 (160, 230)。当它旋转时,它保持左上角位置固定。

(320 像素屏幕宽度 - 460 像素宽度)= -140 像素。因为它居中,所以将其分成两半,得到 -70 像素。与垂直方向相同的想法。

Just a hunch, but I'm guessing prior to the transform that you're setting (or defaulting) the center of landscapeView to (160, 230). When it rotates, it keeps the upper left position fixed.

(320 px screen width - 460 px width) = -140 px. Divide that in half since it's centered, and you get -70 px. Same idea with the vertical.

月依秋水 2024-11-09 07:22:52

70 是宽度和高度之差除以二。 (460 - 320) / 2. 二分法使视图居中。

70 is the difference between the width and height, divided by two. (460 - 320) / 2. The division by two is what centers the view.

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