2012-07-25 71 views
0

我做了一些自定義佈局,包括willAnimateRotationToInterfaceOrientation中的動畫:持續時間: 我遇到的問題是,如果設備從landscapeLeft更改爲landscapeRight,界面應該旋轉,但佈局代碼,尤其是動畫應該不會運行。我如何檢測到它正在從一個景觀變化到另一個景觀? self.interfaceOrientation以及[[UIApplication sharedApplication] statusBarOrientation]不返回有效的結果,他們似乎認爲設備已經旋轉。因此,以下不起作用檢測景觀風景取向變化

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) {...} 
+0

如果不緩存以前的方向值,這似乎不可能。到willAnimateRotationToInterfaceOrientation時:被稱爲接口和設備方向已經更新,這意味着任何方向已經失去了我對它感興趣的點。 – ima747 2012-07-25 20:08:40

回答

1

看來,唯一的解決方案是緩存最後的方向更改。到willAnimateRotationToInterfaceOrientation時:被稱爲設備和接口方向已被更新。解決方案是在每次更改結束時記錄目標方向,以便在方向設置爲再次更改時可以查詢該值。這並不像我希望的那樣優雅(我的視圖控制器上還有另一個屬性),但據我所知,似乎是唯一的方法。

5

您可以檢查設備的方向,然後設置一個標誌,確定您處於左側還是右側。然後,當您的設備切換時,您可以抓住它並根據需要進行處理。

要確定方向使用:

if([UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) 
{ 
    //set Flag for left 
} 
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
{ 
    //set Flag for right 
} 

您還可以趕上的通知時,該設備採用旋轉:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

,然後寫一個方法detectOrientation像這樣:

-(void) detectOrientation 
{ 
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) 
    { 
     //Set up left 
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
    { 
     //Set up Right 
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    { 
     //It's portrait time! 
    } 
} 
+0

問題是,在調用willAnimateRotationToInterfaceOrientation:時,接口和設備方向已經改變,所以你看不到它來自哪裏。 – ima747 2012-07-25 20:07:12

+0

這就是爲什麼我提到設置一個標誌,告訴你你在哪個方向。所以當調用動畫時,你可以檢查你的標誌,並看看你最後的方向。因此,有一個枚舉值,名爲'previousOrientation',其值選項爲'right,left,upsideDown,portrait',並在每次方向更改時更新'previousOrientation'。然後當'willAnimateRotationToInterfaceOrientation:'被調用時,你可以檢查'previousRotation'是什麼。基本上,我在答案中的「設置標誌」的含義就是您在答案中放入的內容,並緩存最後的方向。 – 2012-07-25 20:18:44

3
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight) 
    { 
     NSLog(@"Lanscapse"); 
    } 
    if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    { 
     NSLog(@"UIDeviceOrientationPortrait"); 
    } 
}