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 回答