2010-07-25 58 views
4

我正在運行一個簡單的函數,該函數在多個區域中調用,以幫助在定位更改期間處理iPad應用程序的佈局。它看起來像這樣:UIOrientation返回0或5

- (void) getWidthAndHeightForOrientation:(UIInterfaceOrientation)orientation { 
    NSLog(@"New Orientation: %d",orientation); 
end 

而且我把它在不同的地方是這樣的:

[self getWidthAndHeightForOrientation: [[UIDevice currentDevice] orientation]]; 

功能通常有運行如果方向是縱向還是橫向一些簡單的代碼。不幸的是,當應用程序在第一個位置開始時,它不能按預期工作。結果我得到0。稍後,如果函數以相同的方式調用,但設備從未旋轉過,則返回值爲5.這是什麼意思?爲什麼它會拋出這些值?

總之,爲什麼[[UIDevice currentDevice]方向]會拋出0或5而不是1和4之間的任何值?

UPDATE:

因爲我一直在尋找我的代碼由於道路方向錯誤被處理,我寫了一篇關於如何處理的UIDevice或UIInterface方向明確的崗位:http://www.donttrustthisguy.com/orientating-yourself-in-ios

回答

3

我會建議使用UIDeviceOrientationIsValidInterfaceOrientation(orientation)

它會告訴你,如果它是一個有效的方向(有效期爲橫向或縱向,而不是朝上/朝下/未知)。那麼你可以把它當作它的肖像,如果它的未知。

這是我要做的事:

if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) { 
    // handle landscape 
} else { 
    // handle portrait 
} 
+0

感謝鮑勃!我必須嘗試一下。 – 2012-05-18 21:40:56

+0

很高興爲你效勞! – 2012-05-25 04:35:13

8

你拿看看UIInterfaceOrientation的枚舉值?從文檔:

typedef enum { 
    UIDeviceOrientationUnknown, 
    UIDeviceOrientationPortrait, 
    UIDeviceOrientationPortraitUpsideDown, 
    UIDeviceOrientationLandscapeLeft, 
    UIDeviceOrientationLandscapeRight, 
    UIDeviceOrientationFaceUp, 
    UIDeviceOrientationFaceDown 
} UIDeviceOrientation; 

因此,它可以想象任何從0-6。

編輯:也許您應該使用您的UIViewControllerwillRotateToInterfaceOrientation:duration:等)上的方法,而不是在上調用orientation

+0

一旦我通過旋轉裝置它總是正確改變方向。但我不明白爲什麼它會是Unknown或返回FaceUp。是否有我需要調用的函數,讓它找到目前未知的方向? – 2010-07-25 01:28:57

+0

我假定當設備平行於地面並且尚未旋轉時返回FaceUp。 (我編輯了我的答案一點,因爲顯然我無法計數。) – Wevah 2010-07-25 02:44:34

+0

我已經編輯了我的答案後,再看一些東西了。 (我承認我主要是做桌面的東西。) – Wevah 2010-07-25 02:51:03

0

[UIDevice currentDevice].orientation返回UIDeviceOrientation

屬性的值是表示設備的當前 方向恆定。此值表示設備的物理方向 ,並且可能與您應用程序的用戶界面的當前方向 不同。

getWidthAndHeightForOrientation功能需要一個UIInterfaceOrientation參數:

應用程序的用戶界面的方向。

這些類型雖然相關,但不是相同的東西。您可以使用self.interfaceOrientation從任何視圖控制器訪問當前的接口方向UIInterfaceOrientation有4個可能的值,同時UIDeviceOrientation具有9:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) { 
    UIDeviceOrientationUnknown, 
    UIDeviceOrientationPortrait,   // Device oriented vertically, home button on the bottom 
    UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top 
    UIDeviceOrientationLandscapeLeft,  // Device oriented horizontally, home button on the right 
    UIDeviceOrientationLandscapeRight,  // Device oriented horizontally, home button on the left 
    UIDeviceOrientationFaceUp,    // Device oriented flat, face up 
    UIDeviceOrientationFaceDown    // Device oriented flat, face down 
}; 

// Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa). 
// This is because rotating the device to the left requires rotating the content to the right. 
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { 
    UIInterfaceOrientationPortrait   = UIDeviceOrientationPortrait, 
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, 
    UIInterfaceOrientationLandscapeLeft  = UIDeviceOrientationLandscapeRight, 
    UIInterfaceOrientationLandscapeRight  = UIDeviceOrientationLandscapeLeft 
};