【问题标题】:orientation changed is not working方向改变不起作用
【发布时间】:2012-03-12 07:13:30
【问题描述】:

提前致谢

我想检测我的设备的方向何时改变... 为此我使用了这个监听器

[[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() 方法上的那个方向时......

谁能帮帮我..

【问题讨论】:

  • -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 将在设备方向改变时调用

标签: iphone orientation screen-orientation orientation-changes


【解决方案1】:

你的问题很模糊,这是一个一般性的解释。您可以覆盖下面提到的方法来处理方向变化

覆盖下面提到的方法

- (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 放在桌子上(我曾经遇到过这种情况......非常讨厌的问题)。

apple developer link 将提供有关处理方向的更多信息

【讨论】:

  • 谢谢,很高兴我的回答对您有用。请标记为正确答案;)
【解决方案2】:
Try this
[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
 if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) )
{
    //code here     
}

【讨论】:

    【解决方案3】:

    改变以下内容

    - (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];
      }    
    }  
    

    【讨论】:

      【解决方案4】:

      你为什么不直接使用 shouldAutorotateToInterfaceOrientation ? 它是 UIViewController 中的方法已经被调用...

      【讨论】:

        猜你喜欢
        • 2016-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-21
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        相关资源
        最近更新 更多