旋转时视图移出位置

发布于 2024-10-27 00:25:25 字数 1409 浏览 1 评论 0原文

我创建了一个 UIViewController (基于 如何在旋转时切换视图)来切换设备旋转时在 2 个视图之间切换。每个视图都是针对特定方向“专门化”的。

它使用 UIDeviceOrientationDidChangeNotification 通知来切换视图:

-(void) deviceDidRotate: (NSNotification *) aNotification{

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    NSLog(@"Device rotated to %d!", orientation);

    if ((orientation == UIDeviceOrientationPortrait) ||
        (orientation == UIDeviceOrientationPortraitUpsideDown)) {
        [self displayView:self.portraitViewController.view];
    }else if ((orientation == UIDeviceOrientationLandscapeLeft) ||
               (orientation == UIDeviceOrientationLandscapeRight)) {
        [self displayView:self.landscapeViewController.view];
    }

}

以及其他一些工作。当我旋转到横向然后返回纵向时,问题就会出现。当返回纵向时,子视图不会显示在正确的位置,特别是 UIPickerView:

第一次纵向在此处输入图像描述

旋转到横向在此处输入图像描述

返回肖像在此处输入图像描述

如果我重复旋转过程,事情只会变得更糟。我做错了什么?

源代码在这里: http://dl.dropbox.com/u/ 3978473/forums/Rotator.zip

提前致谢!

I created a UIViewController (based on How to switch views when rotating) to switch between 2 views when the device rotates. Each view is "specialized" for a particular orientation.

It uses the UIDeviceOrientationDidChangeNotification notification to switch views:

-(void) deviceDidRotate: (NSNotification *) aNotification{

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    NSLog(@"Device rotated to %d!", orientation);

    if ((orientation == UIDeviceOrientationPortrait) ||
        (orientation == UIDeviceOrientationPortraitUpsideDown)) {
        [self displayView:self.portraitViewController.view];
    }else if ((orientation == UIDeviceOrientationLandscapeLeft) ||
               (orientation == UIDeviceOrientationLandscapeRight)) {
        [self displayView:self.landscapeViewController.view];
    }

}

and sort of works. The problems shows up when I rotate to Landscape and then back to Portrait. When going back to portrait the subviews aren't displayed in the right place, specially the UIPickerView:

First Time Portrait:
enter image description here

Rotate to Landscape:
enter image description here

Back to Portrait:
enter image description here

If I repeat the rotation process, things just get worse. What am I doing wrong?

The source code is here: http://dl.dropbox.com/u/3978473/forums/Rotator.zip

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚你心 2024-11-03 00:25:25

要解决偏移问题,请重写 displayView: 方法,如下所示。

-(void)displayView:(UIView *)aView{
self.view = aView;
然而

,旋转很奇怪。你应该检查那部分代码。
使用 UIViewController 旋转方法

  • (void)willRotateToInterfaceOrientation:duration:
  • (void)didRotateFromInterfaceOrientation:

而不是 -(void)deviceDidRotate:

更简单,您将避免那种奇怪的弹跳,并且不再需要通知。
阅读有关我上面指定的方法的苹果文档。

希望这有帮助。

To solve your offset problems, rewrite the displayView: method as below.

-(void) displayView: (UIView *)aView{
self.view = aView;
}

Rotations however are strange. you should review that part of code.
Use the UIViewController rotation methods

  • (void)willRotateToInterfaceOrientation:duration:
  • (void)didRotateFromInterfaceOrientation:

instead of -(void)deviceDidRotate:

Much simpler, you will avoid that strange bouncing, and you don't need notifications any more.
Do some reading on the apple documentation on the methods i specified above.

Hope this helps.

司马昭之心 2024-11-03 00:25:25

好的,我发现了错误。这非常简单和愚蠢:我混合了框架和边界。

在 displayView: 代码中,我将子视图的框架设置为父视图的框架,而它应该是父视图的边界。

OK, I found the error. It's pretty simple and stupid: I mixed frame and bounds.

In the displayView: code I was setting the frame of the child view to the frame of the parent view, when it should be the bounds of the parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文