方向更改后,视图控制器对我有什么作用? [英] What does a view controller do for me when orientation changed?

查看:37
本文介绍了方向更改后,视图控制器对我有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由项目模板基于视图的应用程序生成的一个简单的iphone程序,带有几个按钮,我添加了以下代码:

A simple iphone program generated by project template View-based Application, with several buttons, and I added following code:

- (void) showInfo: (UIView *) view {
    NSLog(@"view bounds  %6.2f %6.2f %6.2f %6.2f", view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width, view.bounds.size.height);
    NSLog(@"view frame   %6.2f %6.2f %6.2f %6.2f", view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    NSLog(@"view center  %6.2f %6.2f", view.center.x, view.center.y);
}

- (BOOL)shouldAutorotateToInterfaceOrientation: UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval) duration {
    switch(toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            [self showInfo: self.view];
            break;
        case UIInterfaceOrientationLandscapeLeft:
            [self showInfo: self.view];
            break;
        case UIInterfaceOrientationLandscapeRight:
            [self showInfo: self.view];
            break;
    }
}

- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation {
    switch(fromInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            [self showInfo: self.view];
            break;
        case UIInterfaceOrientationLandscapeLeft:
            [self showInfo: self.view];
            break;
        case UIInterfaceOrientationLandscapeRight:
            [self showInfo: self.view];
            break;
    }
}

在肖像"中启动模拟器,向右更改为风景".我可以得到:

Launch simulator in Portrait, change to landscape right. I can get:

视图范围0.00 0.00 320.00 460.00
视图框架0.00 20.00 320.00 460.00
查看中心160.00 250.00
在纵向模式下,由于状态栏的原因,视图的尺寸为320x460,其原点为(0,20).

view bounds 0.00 0.00 320.00 460.00
view frame 0.00 20.00 320.00 460.00
view center 160.00 250.00
In portrait, view's size is 320x460, and it's origin is (0,20) because of status bar.

视图范围0.00 0.00 480.00 300.00
视图框架0.00 0.00 300.00 480.00
查看中心150.00 240.00
在风景右侧,边界的大小更改为480x300,但是,帧的原点是(0,0).而且框架的大小与界限不同.

view bounds 0.00 0.00 480.00 300.00
view frame 0.00 0.00 300.00 480.00
view center 150.00 240.00
In landscape right, bounds' size is changed to 480x300, However, the origin of the frame is (0, 0). And the size of frame is different from bounds.

在我的脑海中,我想象这些坐标就像下面的图片:

In my head, I imagine that these coordinates are like following pictures:

我的问题是:在风景中,似乎框架的原点指向一个位置,而边界的原点指向另一个位置.因此,我认为视图控制器中的某处会发生旋转.它在哪里,做什么?

My question is: in landscape right, it seems that the frame's origin points to one location while bounds' origin points to a different one. So I think somewhere in a view controller some rotation happens. Where is it and what does it do?

感谢您阅读这个冗长且不清楚的问题. :)

Thanks for reading this long and not-so-clear question. :)

推荐答案

在横向模式下,将调整内容视图"(controller.view)的大小并应用90度变换.

In landscape mode, the "content view" (controller.view) will be resized and applied a transform of 90 degrees.

由于bounds表示内部坐标系中的边界矩形,所以bounds.origin将始终位于视图本身的左上角.

Since bounds represent the bounding rectangle in the internal coordinates system, the bounds.origin will always at the upper-left corner of the view itself.

但是,frame外部或父坐标系中的边界矩形.视图的父级是窗口,该窗口的原点仍位于设备的绝对左上角.因此,您的frame.origin在横向模式下处于该位置.

However, frame is the bounding rectangle in the external, or parent, coordinates system. The parent of your view is the window, which still has the origin in the device's absolute upper-left corner. Hence your frame.origin is at that position in landscape mode.

这篇关于方向更改后,视图控制器对我有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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