如何禁用任何自动旋转?

时间:2010-02-21 06:32:38

标签: iphone

我正在尝试编写从纵向屏幕调用横向屏幕的代码。 当前屏幕方向为横向时,它将不允许任何自动旋转。

我尝试过以下代码:

// Override to allow orientations other than the default portrait orientation.
/*- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}*/

- (void)viewDidLoad {

   [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
   CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
   landscapeTransform = CGAffineTransformTranslate(landscapeTransform, +90.0, +90.0);
   [self.view setTransform:landscapeTransform];
}

但上面的代码允许UIInterfaceOrientationLandscapeRight。

如何禁用任何自动旋转?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您想要在横向模式下启动应用程序吗? 在所有情况下,您必须覆盖shouldAutorotateToInterfaceOrientation方法。

但是如果你想在肖像模式下启动应用程序,然后转到横向模式,你可以添加一个静态布尔值来知道你是否必须保持在landspace模式。 你的代码必须是这样的:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(isInPortraitMode) {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    } else {
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
            isInPortraitMode  = YES;
        }
        return YES;
    }
}

- (void)viewDidLoad {
    isInPortraitMode = NO;
    //...
}