2012-10-10 55 views
1
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    { 
     [self isPortraitSplash]; 
    } 
    else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationIsLandscape(interfaceOrientation)) 
    { 
     [self isLandScapeSplash]; 
    } 
    return YES; 
} 

在我的方法isPortraitSplashisLandScapeSplash,我設置視圖的幀。問題與方向

當方向更改時,它始終呼叫isLandScapeSplash - 無法調用isPortraitSplash方法。

任何人都可以告訴我爲什麼會發生這種情況嗎?

回答

2

您現有的if聲明將BOOLUIDeviceOrientation進行比較。您的測試必須是:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    { 
     [self isPotraitSplash]; 
    } 
    else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) 
    { 
     [self islandScapeSplash]; 
    } 
    return YES; 
} 

UIInterfaceOrientationIsPortraitreturns a BOOL,所以這就是你在你的if語句條件的需要。

更新:我也補充一點,我認爲這是更好地做這項工作在willRotateToInterfaceOrientation:duration:其他的答案一致,而不是shouldAutorotateToInterfaceOrientation:

但是,這不是你原來的代碼失敗的原因。由於if測試將UIDeviceOrientationBOOL比較,原始代碼失敗。

1

首先在

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

必須聲明要支持所有的方向。

- (BOOL)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    { 
     [self isPotraitSplash]; 
    } 
    else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) 
    { 
     [self islandScapeSplash]; 
    } 
} 
你必須設置的框架或任何其他佈局的變化,並使用像上面

2

使用- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration而不是shouldAutorotateToInterfaceOrientation,它保證在發生旋轉之前被調用。

不要刪除shouldAutorotateToInterfaceOrientation,對於想要支持的每個方向返回YES。