如何在旋转仿射变换中摆脱这些神奇的数字?

时间:2022-12-19 23:38:05

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:

我有一个320x460(标准的iPhone屏幕)的视图,我想要绘制,就像它是在横向模式,虽然手机不是这样的方向。这似乎是一个简单的任务,我试图通过创建大小为460x320的子视图,然后通过设置视图转换将其旋转90度来解决。除了旋转的视图没有正确居中之外,这很有效。为了“修复”这个,我添加了一个翻译转换,最终看起来像这样:

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?

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

2 个解决方案

#1


4  

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.

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

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

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

#2


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.

70是宽度和高度之差除以2。 (460 - 320)/ 2.两分之一是观点的中心。

#1


4  

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.

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

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

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

#2


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.

70是宽度和高度之差除以2。 (460 - 320)/ 2.两分之一是观点的中心。