4

向きの変更中にiPadアプリのレイアウトを処理するために、複数の領域で呼び出される単純な関数を実行しています。次のようになります。

- (void) getWidthAndHeightForOrientation:(UIInterfaceOrientation)orientation {
    NSLog(@"New Orientation: %d",orientation);
end

そして、私は次のようにさまざまな場所でそれを呼び出します。

[self getWidthAndHeightForOrientation: [[UIDevice currentDevice] orientation]];

この関数には、通常、向きが縦または横の場合に実行される単純なコードが含まれています。残念ながら、アプリが位置 1 で開始されたときに期待どおりに機能しませんでした。結果として 0 を取得します。後で関数が同じ方法で呼び出されたが、デバイスが一度も回転されていない場合、値 5 が返されます。これはどういう意味ですか? なぜこれらの値をスローするのでしょうか?

要するに、[[UIDevice currentDevice] orientation] が 1 から 4 までの値ではなく、0 や 5 をスローするのはなぜでしょうか?

アップデート:

向きの処理方法が原因でコードにバグを見つけ続けたため、UIDevice または UIInterface の向きを処理する方法に関する決定的な投稿を書きました: http://www.donttrustthisguy.com/orientating-yourself-in-ios

4

3 に答える 3

8

の列挙値を見ましたUIInterfaceOrientationか? ドキュメントから:

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

したがって、0 ~ 6 のいずれかになる可能性があります。

編集: ?を呼び出す代わりに、 UIViewController(willRotateToInterfaceOrientation:duration:など)のメソッドを使用する必要があるかもしれません。orientationUIDevice

于 2010-07-25T00:19:39.433 に答える
3

使用することをお勧めしますUIDeviceOrientationIsValidInterfaceOrientation(orientation)

有効な向きであるかどうかがわかります (FaceUp/FaceDown/UnKnown ではなく、横向きまたは縦向きのいずれかが有効です)。不明な場合は、肖像画として扱うことができます。

これは私がそれを行う方法です:

if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    // handle landscape
} else {
    // handle portrait
}
于 2012-05-15T22:37:03.830 に答える
0

[UIDevice currentDevice].orientationを返しますUIDeviceOrientation:

プロパティの値は、デバイスの現在の向きを示す定数です。この値は、デバイスの物理的な向きを表し、アプリケーションのユーザー インターフェイスの現在の向きとは異なる場合があります。

あなたのgetWidthAndHeightForOrientation関数はUIInterfaceOrientationパラメータを取ります:

アプリケーションのユーザー インターフェイスの向き。

これらのタイプは関連していますが、同じものではありません。を使用して、任意のビュー コントローラーから現在のインターフェイスの向きにアクセスできますself.interfaceOrientationUIInterfaceOrientation可能な値は 4 つあり、 UIDeviceOrientation9 つある:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

// Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).
// This is because rotating the device to the left requires rotating the content to the right.
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};
于 2013-08-06T16:03:50.977 に答える