如何在启动应用程序时锁定方向,并在导航到Viewcontroller时将其解锁

时间:2012-07-04 16:40:00

标签: iphone locking rotation uiinterfaceorientation device-orientation

我想在用户启动应用时向用户显示图片。此图像显示20秒。

我希望有一个功能,当用户在横向启动应用程序时,应用程序将保留在横向。当用户在纵向中推送应用程序时,应用程序将保留在纵向中。

我真的很难在Appdelegate中配置它,所以我制作了一个单独的viewcontroller来显示这个图像。计时器结束后,我导航到应启用旋转的下一个视图。

那么如何暂时锁定iPad的用户界面呢?

编辑:

我通过在Appdelegate之后的第一个Viewcontroller中的viewDidLoad中实现方向检查来解决这个问题。对于每个方向,我都保存了一个值。执行shouldAutorotate时:我首先检查保存的值,该值禁用方向更改。

代码中的解决方案:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"PortraitLandscapeIndicator"];
    }
    else {
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"PortraitLandscapeIndicator"];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //the latest saved orientation is our orientation
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"PortraitLandscapeIndicator"] == 1){
         return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
    }
    else{
        return ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));

    }

}

2 个答案:

答案 0 :(得分:0)

首次显示图像时应保存初始interfaceOrientation(视图控制器上的属性),然后在shouldAutorotateToInterfaceOrientation:方法中,只有在方向与初始方向相同时才返回YES保存值

计时器完成后,您可以允许旋转到任何支持的方向

答案 1 :(得分:0)

听起来你必须做一些特殊的viewController在启动时显示的内容,在loadView或init获取设备方向,然后适当地设置视图框架,并根据任何模式纵向或横向,然后只返回允许来自[shouldAutorotateToInterfaceOrientation:]的方向。

计时器启动后,您可以将控制权传递给其他一些viewController,以便从那里获取或随后允许其他方向。

但最重要的是,我认为你必须通过任何最适合你的方案以编程方式进行。