启动应用程序时检测iPad方向

时间:2012-03-11 14:11:42

标签: ios objective-c ipad device-orientation

有人知道在启动应用时以编程方式检测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 个答案:

答案 0 :(得分: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
}