2012-03-11 65 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 
}