10

これを行うことになりました: 複数の向きをサポートする最も簡単な方法は? アプリケーションがランドスケープの場合、カスタム NIB をロードするにはどうすればよいですか? 素晴らしい作品!

Photoshop で作成した画像を、iPad アプリケーションの情報画面の背景として使用します。画像にはテキストといくつかのアイコンも含まれています。画像の周りに緑色の境界線があります。

私が達成しようとしている効果は次のとおりです。

ユーザーが縦向きから横向きに移動すると、画像 (フレームとアイコンのみ) を 90 度回転させて、フレームを横向きに縦向きに表示するのではなく、横向きモードで表示するようにします。テキストとアイコンは分離されており (異なる UIImageView で編成した異なるレイヤー)、90 度回転します。

私がすでに行ったことは次のとおりです。

この方法で少し実験しました:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:

そしてこれをやろうとしました:

self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);

正のラジアンを指定しているため、btnFacebook プロパティを右に 90 度回転させると思います (間違っている場合は修正してください)。私はそれを正しく動作させることができないようです。これは、フレーム内の中心座標を中心にボタンを 90 度回転させるべきではありませんか? それはボタンの位置の変化を引き起こさないでしょうか(ボタンは四角です)?

編集

画像を作成しました:

ここに画像の説明を入力

画像が画像の背景 (両方の向きで見栄えのするいくつかのカスタム グラフィック) を示しているため、縦向きから横向きになり、回転するため、横向きでは縦向きとして表示されません。アイコンは背景から切り離されているため、次のように回転する必要があります。正しい向きにする必要があるためです(ソーシャルアイコンです)。ただし、テキストは同じ位置にあります。位置を変更せずに 90 度回転するだけです。

4

1 に答える 1

21

インターフェイス全体を回転させるのではなく、紫と赤の正方形を個別に回転させようとしているという質問を理解しています。

UIViewControllerあなたのレイアウトに似た を作成しました。

インターフェイス ビルダのスクリーンショット

黒い四角は UIView で、白い四角は黒いビューがいつ回転するかを知るためだけに存在します。このビューはview1、コントローラーのプロパティに接続されています。

btnx(x run 1 から 4) プロパティに接続されている 4 つのボタンがあります。

インターフェイスを自動回転させたくないので、縦向きのみをサポートします。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

回転を手動で行うために、ViewController にメソッドを追加しました。コンポーネントを縦向きから現在の向きに回転するために必要な角度を決定し、回転変換を作成して、それをすべてのアウトレットに適用します。

- (void)deviceDidRotate:(NSNotification *)notification
{
    UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
    double rotation = 0;
    UIInterfaceOrientation statusBarOrientation;
    switch (currentOrientation) {
        case UIDeviceOrientationFaceDown:
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationUnknown:
            return;
        case UIDeviceOrientationPortrait:
            rotation = 0;
            statusBarOrientation = UIInterfaceOrientationPortrait;
            break;
        case UIDeviceOrientationPortraitUpsideDown:   
            rotation = -M_PI;
            statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
            break;     
        case UIDeviceOrientationLandscapeLeft:     
            rotation = M_PI_2;   
            statusBarOrientation = UIInterfaceOrientationLandscapeRight;
            break;  
        case UIDeviceOrientationLandscapeRight:    
            rotation = -M_PI_2;
            statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
            break;
    }
    CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
    [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        [self.btn1 setTransform:transform];
        [self.btn2 setTransform:transform];
        [self.btn3 setTransform:transform];
        [self.btn4 setTransform:transform];
        [self.view1 setTransform:transform];
        [[UIApplication sharedApplication] setStatusBarOrientation:statusBarOrientation];
    } completion:nil];
}

最後に行うことは、OS にメソッドを呼び出させることです。application:didFinishLaunchingWithOptions:それを実現するために、AppDelegate に次のコードを追加しました。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self.viewController selector:@selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

これがまさにあなたが望んでいたものかどうかはわかりませんが、少なくとも似ているので、問題を解決する方法についていくつかのアイデアを得ることができると思います. これを説明するために作成した動作中の iPad アプリケーションのソース コードを提供できます。

于 2011-06-20T09:56:04.297 に答える