2011-08-19 77 views
0

我使用以下在网络中找到的代码将画面旋转到风景模式。我不明白他们想做什么。特别是它设定的范围。有人可以解释它在做什么吗?将画面从肖像旋转到风景

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) 
{ 
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; 

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 


    UIScreen *screen = [UIScreen mainScreen]; 
    CGRect newBounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width - statusBarFrame.size.height); 



    self.navigationController.view.bounds = newBounds; 
    self.navigationController.view.center = CGPointMake(newBounds.size.height/2.0, newBounds.size.width/2.0); 
    self.navigationController.view.transform = CGAffineTransformConcat(self.navigationController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(90))); 
    self.navigationController.view.center = window.center; 
} 
+3

你一定会觉得很更好的服务让窗口和UIViewControllers应用变换自己,然后处理任何看法改变了你通过实施具体需要视图控制器上的适当方法。 –

+0

根据我们的要求,我们必须手动完成 – Janaka

+0

手动将转换应用到导航控制器没有什么好的理由。你最终会在脚下射击自己。但是,它正在做的是将转换矩阵应用于90度旋转的视图。如果你这样做,你的边界将会出错(对于新的屏幕高度它们将会太长,对于新的屏幕宽度它们将会太窄)上面的代码更新了边界以适应新的屏幕,将中心重新定位到新的逻辑屏幕中心将应用围绕视图中心的旋转,然后将中心移回屏幕中心 –

回答

0
曾经是(320,480),例如,旋转后,你应该把它为了适应在横向模式下的屏幕设置为(480,320)

你rootView的大小,这就是为什么你需要改变你的观点的界限。

设定的变换使得观点实际上旋转90度。

UIKit的是在做类似的事情时,会自动为你转动。

+0

所以边界设置新的显示区域? – Janaka

+0

是的。通常,根视图应始终与屏幕大小相同。 – xuzhe

+0

@Janaka如果你认为答案确实有用,投票并接受是非常感激的。 – xuzhe

0

你好Janaka,

  You will try this code. 

     Take a look at the function `shouldAutorotateToInterfaceOrientation`: in the UIViewController class. This function returns YES if the orientations is supported by your UIView. If you return YES only to the landscape orientation, then the iPhone will automatically be put in that orientation. 

下面的代码应该这样做。把它放在UIViewController中,该UIViewController控制你想放在横向模式下的视图。

使用此方法。 (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {//对于支持的方向返回YES return(interfaceOrientation == UIInterfaceOrientationLandscape); }

尝试此链接的绝对帮助你 this

+0

我希望视图永久处于横向模式。我不希望它是基于设备的变化。 – Janaka

+0

在我的代码中编辑 –

0
#define degreesToRadians(x) (M_PI * x/180.0) 

- (void)viewWillAppear:(BOOL)animated 
{ 

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; 

    CGRect newBounds = CGRectMake(0, 0, 480, 320); 
    self.navigationController.view.bounds = newBounds; 
    self.navigationController.view.center = CGPointMake(newBounds.size.height/2.0, newBounds.size.width/2.0); 

    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90)); 

    [super viewWillAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    self.navigationController.view.transform = CGAffineTransformIdentity; 
    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0)); 
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320.0, 480.0); 

    [super viewWillDisappear:animated]; 
}