“不正确"在 iPhone 中重新定向后的框架/窗口大小 [英] "Incorrect" frame / window size after re-orientation in iPhone

查看:18
本文介绍了“不正确"在 iPhone 中重新定向后的框架/窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iPhone OS 应用程序中,我希望(需要)观察设备方向的变化,以便重新排列屏幕的某些部分.我使用的方法是使用 CGRect frame = [UIScreen mainScreen].applicationFrame 来获取屏幕大小,并从那里计算其他控件的大小和/或定位(我也尝试了 self.view.frame).

In my iPhone OS application I want (need) to watch for changes in the device orientation in order to rearrange certain portions of the screen. The approach I used was to use CGRect frame = [UIScreen mainScreen].applicationFrame to get the screen size, and from there calculate the size and / or positioning of other controls (I also tried self.view.frame).

到目前为止,所有测试都是在纵向模式下完成的,因此我可以专注于编写主要功能,然后只对横向进行一些调整.这里出现了问题:在 -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 我添加了一些日志记录以在继续之前检查大小,但显然 Width 和 Height 的值是错误的"(我说错误",因为乍一看这些值对我来说没有意义).

All testing was done so far in Portrait mode, so I could focus on programming the main features and later on just do some adjustments for Landscape. And here enters the problem: In -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation I added some logging to check the sizes before proceeding, but apparently the values for Width and Height are "wrong" (I say "wrong" because at a first glance the values does not make sense to me).

这是一些日志的输出:

  • 旋转:横向 [w=300.000000,h=480.000000]
  • 旋转:纵向 [w=320.000000,h=460.000000]

Landscape 中w"和h"的值在我看来是颠倒的——我期待 w=480 和 h=300.

The values for "w" and "h" in Landscape seem inverted to me - I was expecting that w=480 and h=300.

我做错了什么?我用来调试的代码如下.

What am I doing wrong? The code I used to debug is below.

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize size = frame.size;

    NSLog(@"%@", [NSString stringWithFormat:@"Rotation: %s [w=%f, h=%f]",  
        UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? "Portrait" : "Landscape",
        size.width, size.height]);
}

推荐答案

设备的方向发生了变化,而不是屏幕的物理特性.您基本上将它倾斜到一边,但实际上它的宽度为 320 像素(由于状态栏显示,您目前无法使用其中的 20 个像素)和 480 像素高.如果您的视图是自动旋转的,那么宽度/高度已为您转换,但是当您询问屏幕的实际尺寸时,您会得到屏幕的实际尺寸.

The orientation of your device changed, not the physical characteristics of the screen. You basically tipped it on its side, but in reality it is 320 pixels wide (20 of which are not available to you at the moment since the status bar is showing) and 480 pixels tall. If your view is auto-rotating, then the width/height have been translated for you, but when you ask for the actual dimensions of the screen, you get back the actual dimensions of the screen.

这也是为什么在处理转换视图时,重要的是根据视图的中心和视图的边界而不是视图的框架进行计算.

This is also why, when working with translated views, it is important to do calculations based on the view's center and the view's bounds and never on the view's frame.

这篇关于“不正确"在 iPhone 中重新定向后的框架/窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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