2012-10-01 12 views
12

デバイスを横向きまたは横向きのいずれかの向きにするゲームを作成しています。プレイヤーは一時停止中に向きを変更できます。彼らがそうするとき、私は、ゲームが向きに基づいて加速度計を解釈する方法を変更する必要があります。iOS 6 - 向きが変わったときにカスタムコードを実行する方法

iOS 5では、私はwillRotateToInterfaceOrientationを使用して変更をキャッチして変数を変更しましたが、これはiOS6では廃止されました。私の既存のコードは次のようになります。

if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
     rect = screenRect; 

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){ 
     rect.size = CGSizeMake(screenRect.size.height, screenRect.size.width); 
    GameEngine *engine = [GameEngine sharedEngine]; 
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){ 
     engine.orientation = -1; 
    } else { 
     engine.orientation = 1; 
    } 
} 

私は交換がのUIViewControllerクラスのviewWillLayoutSubviewsメソッドであることを理解しています。私はcocos2d 2.1でこのゲームを構築していますが、デモプロジェクトにはUIViewControllerクラスがないようですので、組み込む方法やコードをどのように見てこの作業を行うべきかについてはっきりしていません。

答えて

36

は、デバイスの向きの変更を聞く:

通知
[[NSNotificationCenter defaultCenter] 
     addObserver:self 
      selector:@selector(deviceOrientationDidChangeNotification:) 
       name:UIDeviceOrientationDidChangeNotification 
      object:nil]; 

は、UIDeviceからデバイスの向きを取得:特定のタスクのための

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    switch (orientation) 
    { 
     // etc... 
    } 
} 
+3

+1正しいオプション。フェイスアップとダウンの向きの変更について通知され、 'UIInterfaceOrientation ...'と 'UIDeviceOrientation ...' – Till

+0

@Darrenの違いに気をつけるように注意してください。ありがとう! –

+0

@Till - フェイスアップとダウンの違いは、人物が肖像画や肖像画を逆さまにしたときのことですか?または、他の何か?また、DeviceOrientationとInterfaceOrientationは、複数のビューなどの特殊な状況やInterfaceOrientationがデバイス方向に一致しないようにプログラムで設定されている場合を除いて、ほとんど同じであると仮定しました。または、それが問題を引き起こす可能性があることを目の当たりにする他のインスタンスがありますか? –

関連する問題