当方向改变时,视图控制器对我做了什么?

yeh*_*nan 4 iphone

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

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在纵向中启动模拟器,更改为横向右侧.我可以得到:

查看范围0.00 0.00 320.00 460.00
查看框0.00 0.00.00 320.00 460.00
查看中心160.00 250.00
在纵向中,视图的大小为320x460,由于状态栏,它的原点是(0,20).

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
在横向右侧,边界的大小更改为480x300,但帧的原点是(0,0).框架的大小与边界不同.

在我的脑海中,我想这些坐标如下图所示:

肖像

肖像

我的问题是:在横向右侧,似乎框架的原点指向一个位置,而边界的原点指向另一个位置.所以我认为视图控制器中的某个位置会发生一些旋转.它在哪里,它做了什么?

感谢您阅读这个长期且不那么明确的问题.:)

ken*_*ytm 7

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

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

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

  • 这是一个比Apple自己的类文档提供的框架与边界更好的总结.在我阅读KennyTM的帖子之前,我无法理解为什么框架的高度和宽度与设备的方向无关.(结果,我了解到我一般都希望坚持布局关于边界的子视图,而不是父母的框架.) (2认同)