开发者

How do I get rid of these magic numbers in my rotation affine transform?

开发者 https://www.devze.com 2023-02-27 01:38 出处:网络
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 so

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. 开发者_StackOverflow中文版 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消