2012-11-01 26 views
1

我需要知道何時從縱向切換到橫向,反之亦然。 如果我選擇傾聽UIDeviceOrientationDidChangeNotification我正面朝上,面朝下等,但只有在有界面旋轉時才需要運行代碼。IOS6我怎樣才能聽取界面方向。不是設備方向

我能做的http://the.ichibod.com/kiji/how-to-handle-device-rotation-for-uiviews-in-ios/

- (void)deviceOrientationDidChange:(NSNotification *)notification { 

//Obtaining the current device orientation 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

//Ignoring specific orientations 
    if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown  || orientation == UIDeviceOrientationUnknown || currentOrientation == orientation) { 
    return; 
    } 

    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(relayoutLayers) object:nil]; 
    //Responding only to changes in landscape or portrait 
    currentOrientation = orientation; 
} 

什麼是最好的方法?

回答

2

我在使用設備方向時遇到類似的問題,導致我的視​​圖。它有上,下,左,右,但也是未知的,另一個我尚未弄清楚它的含義(枚舉中的int 5,0是未知的)。無論如何,我最終採取的是檢測狀態欄的方向。對於用戶所看到的內容而言,這仍然更加一致。

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

希望這會有所幫助。

+0

真的很好!但是我仍然需要保存最後的方向,以便比較並確保它是從縱向到橫向而不是從正面到景觀的旋轉? – Toydor

+0

只需將先前的方向狀態存儲爲類var,那麼當您的視圖出現時,請保存它所在的方向。然後在deviceorientationdidchange上執行它。 – mashdup

+0

所以蘋果不贊成willrotatetointerfaceorientation,他們沒有任何好的選擇。我的天! – Toydor