2011-08-19 135 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]; 
}