2011-11-13 7 views
0

現在、私はiPad用の描画アプリで作業しています。私は同じ場所に描画エリアを維持しながら、それが異なる向きに置かれているときに、アプリケーション内のツールバーを再配置して回転させる必要があります。サイドローテーションによる手動回転方法

これを行うには、hereメソッドが見つかりました。回転の変化を監視するためにNSNotificationCenterを使用します。これはカスタムのdidRotate:メソッドを呼び出し、UIDeviceOrientationに基づいてツールバーを回転させ、位置を変更します。

この部分は正常です。ただし、iPadのサイドスイッチが方向を固定するために使用されている場合は、いつでもツールバーの位置を変更することができます。

たとえば、アプリケーションを横向きに開始して縦向きに回転すると、ツールバーは画面の下に移動します。しかし、スライドスイッチを操作するとすぐに、画面の横方向に画面の横に移動します。

私がこれに使用している方法は以下の通りです。

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

- (void)didRotate:(NSNotification *)notification { 
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

    UIInterfaceOrientation interfaceOrientation; 

    bool orientationFound = YES; 

    if (deviceOrientation == UIDeviceOrientationPortrait) { 
     interfaceOrientation = UIInterfaceOrientationPortrait; 
    } else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown) { 
     interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown; 
    } else if (deviceOrientation == UIDeviceOrientationLandscapeLeft) { 
     interfaceOrientation = UIInterfaceOrientationLandscapeRight; 
    } else if (deviceOrientation == UIDeviceOrientationLandscapeRight) { 
     interfaceOrientation = UIInterfaceOrientationLandscapeLeft; 
    } else { 
     orientationFound = NO; 
    } 



    if (orientationFound) { 
     [self.toolbar changeToOrientation:interfaceOrientation withDuration:.25]; 
     [self.tutorialOverlay changeToOrientation:interfaceOrientation]; 
    } 
} 

- (void)changeToOrientation:(UIInterfaceOrientation)orientation withDuration:(float)duration { 
    float angle; 
    CGPoint origin; 

    if (orientation == UIInterfaceOrientationPortrait) { 
     angle = portraitAngle; 
     origin = self.portraitOrigin; 
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     angle = portraitUpsideDownAngle; 
     origin = self.portraitUpsideDownOrigin; 
    } else if (orientation == UIInterfaceOrientationLandscapeLeft) { 
     angle = landscapeLeftAngle; 
     origin = self.landscapeLeftOrigin; 
    } else if (orientation == UIInterfaceOrientationLandscapeRight) { 
     angle = landscapeRightAngle; 
     origin = self.landscapeRightOrigin; 
    } 

    [UIView animateWithDuration:duration animations:^{ 
     self.transform = CGAffineTransformMakeRotation(angle); 

     CGRect rect = self.frame; 

     rect.origin = origin; 

     self.frame = rect; 
    }]; 
} 

答えて

1

この通知ベースのアプローチを使用することを強くお勧めします。通知の名前はUIDeviceOrientationDidChangeNotificationです。デバイスの向きはインターフェイスの向きと同じではなく、このような問題はほとんどありません。 (デバイスの向きは、例えば、インターフェースの向きに関連付けされることはないUIDeviceOrientationFaceDownを含む。)

私はあなたのビューコントローラが自動的に回転させることをお勧めしたいです。これはあなたのためのツールバーなどを配置します。次に- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)durationを上書きして、図面領域を背面に保存する方向に置きます。 (あなたは上記のごchangeToOrientation方法に類似したコードを使用すると思いますが、代わりに描画領域のために、そしてあなた自身のアニメーションブロックを作成する必要はありません。また、角度はすべて否定されるだろう、あなたは変更元に戻すしているので、表示コントローラを作成しました)

+0

私は前にその方法を試してみたとの通知ベースのアプローチを使用した場合、その後、さらに問題がありました。たとえば、描画領域の境界とフレームプロパティが一致しない場合があります。また、異常な描画異常も発生しました。 – robhasacamera

+0

あなたは正しいです。ビューにトランスフォームがあるときはフレームが定義されていないので、トランスフォームする場合は境界に頼らざるを得ません。 –

1

私はちょうど同様の質問hereに答えました。 基本的にはインターフェイスの回転のみを許可しますが、回転させたくないビューはwillRotateToInterfaceOrientation:duration:で回転させます。

関連する問題