0

有谁知道在启动应用程序时以编程方式检测 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;
   }
}

但是当它与地板平行放置时,它无法检测到设备的方向。所以,我正在寻找另一种解决方案。请帮忙......

4

1 回答 1

1

查看文档中的UIDeviceOrientation 枚举

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
}
于 2012-03-11T14:32:24.123 回答