【问题标题】:populate entire landscape screen with new uiview upon device orientation change from portrait to landscape在设备方向从纵向更改为横向时使用新的 uiview 填充整个横向屏幕
【发布时间】:2012-06-13 01:21:35
【问题描述】:

所以我的 iPhone 应用程序当前有一个填充整个屏幕的 tabviewcontroller。该应用程序仅在纵向模式下运行。我的任务是检测设备方向的变化,一旦它变为横向,就会有一个新的 uiview 填充整个屏幕。

我已经让设备方向变化检测工作。一旦检测到方向变化,我已经使用 NSNotificationCenter 成功调用了辅助方法 deviceOrientationChanged。如果更改为横向模式,我会运行某个代码块。

在这段代码中,我已经尝试了各种方法,但都没有成功。简单地说 self.view = newViewThing;不起作用,因为状态栏仍然存在于顶部并且选项卡仍然存在于底部。 我还尝试将此 newViewThing 作为子视图添加到 UIWindow。这不起作用,因为在添加视图时,它的方向不正确。

问题是:一旦检测到设备方向变化,有没有办法加载全新的 uiview?提前谢谢你。

【问题讨论】:

  • 听起来您并没有替换 tabviewcontroller,而只是更改了它的子视图。 “自我”所指的对象是什么?你能分享你的代码吗?

标签: iphone ios uiview rotation orientation


【解决方案1】:

是的,有一种方法可以加载新视图。我是这样在我的应用中实现的:

- (void)orientationChanged:(NSNotification *)notification
{
    // We must add a delay here, otherwise we'll swap in the new view
    // too quickly and we'll get an animation glitch
    [self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeView animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

我也将此代码添加到viewDidLoad

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification object:nil];

将此代码发送至dealloc:

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

【讨论】:

  • 感谢 Maxim 的解决方案。这是一个功能性的解决方案!然而,从纵向到横向的过渡有点粗糙,因为在方向改变时,应用程序首先旋转已经存在的第一个视图,然后在其位置弹出第二个视图。 有什么办法可以让过渡不那么尴尬?例如,有没有办法在弹出第二个视图之前不旋转第一个视图,或者类似的东西?
猜你喜欢
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多