2010-02-21 91 views
0

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

我试过下面的代码:

// 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。

如何禁用任何自动旋转?

在此先感谢。

回答

0

你想在横向模式下启动你的应用吗? 在所有情况下,您必须重写shouldAutorotateToInterfaceOrientation方法。

但是,如果你想在纵向模式下启动你的应用程序,然后进入横向模式,你可以添加一个静态布尔值来知道你是否必须停留在空间模式下。 您的代码必须如下所示:

- (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; 
    //... 
} 
+0

感谢您的回复。旧版本更好。 但是,这允许纵向模式的肖像模式。我试图让它不会有任何方向。 周期是:开始 - >肖像 - >风景 - >结束。 – Ferdinand 2010-02-22 03:33:59