2014-11-06 35 views

回答

1

您是否嘗試過使用

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

的設備方向這隻能迎合,所以如果你有其他方位禁用。它仍然給正確的值,對於這個曾經向您的設備。您可以其值與(和更多,你可以查看那些https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/index.html#//apple_ref/c/tdef/UIDeviceOrientation

UIDeviceOrientationPortrait, 
UIDeviceOrientationPortraitUpsideDown, 
UIDeviceOrientationLandscapeRight, 
UIDeviceOrientationLandscapeLeft 

我希望這有助於。

編輯:

對於必須添加一個觀察者,檢測設備的方向的情況下被更改。在appdelegate中註冊通知/觀察者,或者在你覺得需要的地方註冊通知/觀察者,並將orientationChanged方法放在你委派觀察者的類中。 (在大多數情況下可能在同一班)。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification 
    object:[UIDevice currentDevice]]; 

- (void) orientationChanged:(NSNotification *)note 
{ 
    NSLog(@"orientationChanged"); 
    UIDevice * device = note.object; 
    if(device.orientation == UIDeviceOrientationPortrait) 
    { 

    } 
    // add other else if here 
    else 
    { 

    } 
} 

請注意,當您添加觀察者時會調用orientationChanged。只有一次。每次您的設備改變方向後。如果您發現有些混淆,請告訴我。

+0

我會給這個鏡頭並報告回來。如果能夠在它發生變化時能夠收到一條消息,那將是很好的,但至少在這一點上我可以調查這個價值。 – BigOmega 2014-11-06 15:11:39

+0

我編輯了我的答案,以便爲正在更改的設備方向添加事件。我最初並沒有注意到你想要一個事件,而不僅僅是一種找到設備當前方向的方法。但看看編輯的答案,看看是否有幫助。 – Abdul91 2014-11-07 06:13:08

+0

工作完美,謝謝! – BigOmega 2014-11-07 16:54:50

相關問題