0

向きが変わったときに UIView に加えられるいくつかの変更があります。これはうまくいきます。電話の向きが既に切り替えられた後にビューを追加すると、問題が発生します。これにより、回転メソッドが呼び出されないため、変更を加える機会がありません。

おそらくViewDidLoadで、これを処理する正しい方法は何でしょうか? その時点で現在の向きを検出できますか?

いくつかの小さな変更を加える必要があることを心に留めておいてください。そのため、別のペン先などをロードしたくありません

どうもありがとうございます :)

編集* 物事を明確にするためだけに: 前述したように、デバイスの向きが変更されたとき、ビューはまだインスタンス化されていません。向きが横向きに変わります -> ユーザーが別のビューを表示するボタンをクリックします -> この新しいビューが作成されて表示されますが、デフォルトの位置は縦向きです -> ビューが表示されると、 willAnimateRotationToInterfaceOrientation メソッドで要素を再配置します間違った位置にいます。

4

1 に答える 1

1

通常、ユーザーがデバイスを回転させたときに発生するアニメーション(主にビューのフレームを操作)をwillAnimateToInterfaceOrientationメソッドに配置します。スケルトン形式では、次のようになります。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    //NSLog(@"willAnimateRotationToInterfaceOrientation: %d", toInterfaceOrientation);

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        // portrait
    }
    else
    {
        // landscape
    }
}

編集:将来の使用のためにデバイスの回転を覚えておく必要がある状況では、currentOrientation(タイプint)と呼ばれるビューコントローラークラスにivarを設定し、次のようにします。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    //NSLog(@"shouldAutorotateToInterfaceOrientation: %d", toInterfaceOrientation);

    if (toInterfaceOrientation == UIDeviceOrientationPortrait || toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown ||
        toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
    {
        currentOrientation = toInterfaceOrientation;
    }

    return YES;
}

次に、View Controllerでメソッドを実行しているときに、デバイスがどの方向にあるかがわかります。

于 2011-09-06T19:34:00.690 に答える