2010-08-01 34 views
4

我正在偵聽UIDeviceOrientationDidChangeNotification,以便根據設備的方向調整我的UI。由UIDeviceOrientationDidChangeNotification返回的不正確的設備方向

問題是我從通知中獲取的設備方向似乎不準確。如果我以縱向和垂直方向(如同拍攝照片一樣)從手機開始,則從通知中獲得的方向是正確的。隨着手機的傾斜接近水平(如平躺在桌面上),方向切換到橫向。這種情況發生在手機實際平放在桌上之前。而這完全沒有將手機轉向風景。就好像它對風景有偏好。

當使用其他應用程序,如郵件,我沒有看到這種相同的行爲。看來郵件只有在確定你已經進入新的方向後纔會切換方向。

任何幫助非常感謝。

感謝,

+0

您能分享您用於回覆通知的代碼嗎? – nschum 2010-08-01 11:06:26

回答

3

如果您唯一的興趣是您的界面方向(即設備的橫向或縱向),則不應使用UIDevice:orientation(或UIDeviceOrientation *常數,如果您願意),而是使用UIApplication:statusBarOrientation使用UIInterfaceOrientation *常量。

我用下面的代碼來檢查景觀作案:

static inline bool isOrientationIsLandscape() { 
    return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]); 
} 

而對於人像作案:

static inline bool isOrientationIsPortrait() { 
    return UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]); 
} 

這花了我整整一個上午搞清楚,因爲沒有UIDeviceOrientationFace [在模擬器上,只能在真實的設備上進行[向上|向下]。所以我的應用程序一直在模擬器上工作,但在實際的設備上,我偶爾會在測試過程中遇到一些未定義的行爲。

現在它在仿真器上的實際設備上工作。

HTH

1

的通知只是一個得到它的方式,你也可以讀出加速自己和正是你認爲合適的方式實現它(與延遲和非一定時間例如旋轉)。

不知道是否能夠獲取這些讀數,但如果是這樣的話,請使用通知來了解事物何時移動,然後啓動加速計讀數。

9

我發現我的問題。

在viewWillAppear中我有:

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

這裏是通知處理程序:

- (void) didChangeOrientation: (id)object { 
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation]; 
    //DLog(@"val is %i", interfaceOrientation); 
    if (interfaceOrientation == UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight || interfaceOrientation == UIDeviceOrientationPortrait) { 
    [self orientInterface:interfaceOrientation]; 
} 

通過檢查方向是兩個景觀之一或兩個人像我忽略了UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown方向。通過這種方式將手機設置爲這兩種方向之一不會影響我的界面方向。

我的問題是,我沒有考慮faceUp和faceDown,並正在假設景觀的其他語句處理它們。

相關問題