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;

これが と を定義していることに注意してUIDeviceOrientationFaceUpくださいUIDeviceOrientationFaceDown。残念ながら、ポートレートまたはランドスケープの場合のように、デバイスがこれらの向きのいずれかにあるかどうかをチェックする組み込みのチェックはありません。ただし、単純な 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 に答える