1

前もって感謝します

デバイスの向きがいつ変更されたかを検出したい...そのために、このリスナーを使用しました

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

方向変更方法は

- (void) orientationChanged:(id)obj
{  
    if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight )
    {
        self.view=self.landscapeView;
        NSLog(@"landscapeView");

       //[self loadView1];
    }
    else 
    {
       self.view = self.portraitView;
       NSLog(@"portraitView");

       //[self loadView1];
    }    
}

そして、ViewDidLoad()メソッドにあったその向きに入るたびに...

誰でも私を助けることができます..

4

4 に答える 4

6

あなたの質問は非常に漠然としていました。これは一般的な説明です。以下のメソッドをオーバーライドして、向きの変更を処理できます

以下のメソッドをオーバーライドする

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // return YES to supported orientation.
}

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
/* Subclasses may override this method to perform additional actions immediately 
   prior to the rotation. For example, you might use this method to disable view 
   interactions, stop media playback, or temporarily turn off expensive drawing or live 
   updates. You might also use it to swap the current view for one that reflects the new
   interface orientation. When this method is called, the interfaceOrientation property 
   still contains the view’s original orientation. 
*/

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    /*Subclasses may override this method to perform additional actions immediately after
      the rotation. For example, you might use this method to reenable view interactions, 
      start media playback again, or turn on expensive drawing or live updates. By the time 
      this method is called, the interfaceOrientation property is already set to the new 
      orientation.*/
}


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    /*This method is called from within the animation block used to rotate the view. 
      You can override this method and use it to configure additional animations that should 
      occur during the view rotation. For example, you could use it to adjust the zoom level 
      of your content, change the scroller position, or modify other animatable properties
      of your view.*/
}

このようにviewDidLoadまたはviewDidAppearでデバイスの向きを確認しないでください

    if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
OR if(UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))

多くの場合、iPhone/iPad をテーブルの上に置いた場合など、予期しない結果が生じるためです (私はこれを経験しました... 非常に厄介な問題です)。

この アップル開発者リンクは、向きの処理に関する詳細情報を提供します

于 2012-03-12T07:33:48.757 に答える
1

以下を変更します

- (void) orientationChanged:(id)obj
{  

  if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight )
  {
    self.view=self.landscapeView;
    NSLog(@"landscapeView");

    //[self loadView1];
  }
  else 
  {
    self.view = self.portraitView;
    NSLog(@"portraitView");

    //[self loadView1];
  }    
}  
于 2012-03-12T07:26:48.553 に答える
1
Try this
[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) )
{
    //code here     
}
于 2012-03-12T07:38:51.030 に答える
0

なぜあなたはただ使わないのですshouldAutorotateToInterfaceOrientationか?すでに呼び出されているのは UIViewController のメソッドです...

于 2012-03-12T07:27:43.983 に答える