2012-01-07 41 views
0

我有一個在故事板模式下創建的通用應用程序。它設置爲自動旋轉到當前的方向。我試圖在iPhone模式下禁用某些方向,但在模擬器中測試時不起作用。故事板中的旋轉和方向問題

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// Return YES for supported orientations 
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
    return ((interfaceOrientation != UIInterfaceOrientationLandscapeRight) || (interfaceOrientation != UIInterfaceOrientationLandscapeLeft)); 
} else { 
    return YES; 
} 
} 

此外,當iPad旋轉時,它應該通過下面的代碼,但我得到的只是一個黑屏。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     self.view = landscape; 
     self.view.transform = CGAffineTransformMakeRotation(deg2rad*(90)); 
     self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0); 
    } else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
     self.view = landscape; 
     self.view.transform = CGAffineTransformMakeRotation(deg2rad*(-90)); 
     self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0); 
    } else { 
     self.view = portrait; 
     self.view.transform = CGAffineTransformMakeRotation(0); 
     self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0); 
    } 
} else { 

} 
} 

爲什麼所有這些問題都出現在這裏?我已經嘗試了不同的Xcode項目中的代碼,沒有故事板廣告,它工作正常。發生了什麼事情,我該如何解決這個問題?

回答

3

您應該創建兩個單獨的計算器問題。我會在這裏回答你的第一個問題。

考慮這條線從您的代碼:

return ((interfaceOrientation != UIInterfaceOrientationLandscapeRight) || (interfaceOrientation != UIInterfaceOrientationLandscapeLeft)); 
  • 如果方向爲Right,那麼你的代碼減少到return (Right != Right) || (Right != Left),它總是返回true。

  • 如果方向爲Left,那麼您的代碼將減少爲return (Left != Right) || (Left != Left),它始終返回true。

  • 如果方向爲Up,那麼您的代碼將減少爲return (Up != Right) || (Up != Left),它總是返回true。

  • 如果方向爲Down,那麼您的代碼將減少爲return (Down != Right) || (Down != Left),該值總是返回true。

目前尚不清楚您希望允許哪些方向以及要排除哪些方向。如果你想只允許縱向模式,這樣做:如果你希望只允許橫向放置

return UIInterfaceOrientationIsPortrait(interfaceOrientation); 

,這樣做:

return UIInterfaceOrientationIsLandscape(interfaceOrientation);