2012-03-11 63 views
0

是否有人知道如何在啓動應用程序時以編程方式檢測iPad方向。啓動應用程序時檢測iPad方向

我正在使用以下機制。

- (void) detectDeviceInitialOrientation 
{ 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    MyAppDelegate *appDelegate=(MyAppDelegate*)[[UIApplication sharedApplication] delegate]; 

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    if (UIDeviceOrientationIsPortrait(orientation)) { 
     appDelegate.orintation = PORTRAIT; 
    } 
    else if (UIDeviceOrientationIsLandscape(orientation)) { 
     appDelegate.orintation = LANDSCAPE; 
    } 
} 

但是,當它平行於地面放置時,它無法檢測到設備的方向。所以,我正在尋找另一種解決方案。請幫忙......

回答

1

看看文檔中的UIDeviceOrientation enum

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

注意,這個定義UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown。不幸的是,沒有內置的檢查設備是否處於其中一個方向,如縱向或橫向。但是,您可以使用簡單的if語句來檢查自己。類似這樣的:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown) { 
    // device is flat on the ground 
} 
相關問題