監聽iOS檢測屏幕旋轉狀態,不需開啟屏幕旋轉


  1. -(void)rotation_icon:(float)n {  
  2.       
  3.     UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>][self.view viewWithTagName:@"home_history"];  
  4.     UIButton *cam_btn = [self.view viewWithTagName:@"cam_btn"];    UIButton *cut_btn = [self.view viewWithTagName:@"cut_btn"];      UIButton *light_btn=[self.view viewWithTagName:@"light_btn"];  
  5.     history_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  6.     cam_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  7.     cut_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  8.     light_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);  
  9. }  
  10. - (void)orientationChanged:(NSNotification *)note  {      UIDeviceOrientation o = [[UIDevice currentDevice] orientation];  
  11.     switch (o) {  
  12.         case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom  
  13.             [self  rotation_icon:0.0];  
  14.             break;  
  15.         case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top  
  16.             [self  rotation_icon:180.0];  
  17.             break;  
  18.         case UIDeviceOrientationLandscapeLeft:      // Device oriented horizontally, home button on the right  
  19.             [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];  
  20.   
  21.             [self  rotation_icon:90.0*3];  
  22.             break;  
  23.         case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left  
  24.             [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];  
  25.   
  26.             [self  rotation_icon:90.0];  
  27.             break;  
  28.         default:  
  29.             break;  
  30.     }   
  31.  }  
  32.   
  33.   
  34. -(void)viewWillDisappear:(BOOL)animated {      
  35.   
  36.   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];  
  37.     UIDevice *device = [UIDevice currentDevice]; //Get the device object  
  38.     [nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];  
  39. }  
  40.   
  41.  - (void)viewDidAppear:(BOOL)animated {   
  42.      // Do any additional setup after loading the view from its nib.         
  43.  //----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----  
  44.     UIDevice *device = [UIDevice currentDevice]; //Get the device object  
  45.     [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation  
  46.     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app  
  47.     [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:device];  
 
======================
 
iPad iPhone  屏幕旋轉檢測的方法
 

在特別的場景下,需要針對屏幕旋轉作特殊處理。在ios系統下實現相關的功能還是比較方便的。

我下面介紹兩種方法:

1.注冊UIApplicationDidChangeStatusBarOrientationNotification通知(舉例:在一個viewcontroller類的viewdidload中注冊該通知),示例代碼如下:

 

        [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(statusBarOrientationChange:)name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

 

- (void)statusBarOrientationChange:(NSNotification *)notification

{

    

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeRight// home鍵靠右

    {

        //

    }

    

    if (

        orientation ==UIInterfaceOrientationLandscapeLeft// home鍵靠左

    {

        //

    }

    

    if (orientation == UIInterfaceOrientationPortrait)

    {

        //

    }

 

    if (orientation == UIInterfaceOrientationPortraitUpsideDown)

    {

        //

    }

}

注意這種方式監聽的是StatusBar也就是狀態欄的方向,所以這個是跟你的布局有關的,你的布局轉了,才會接到這個通知,而不是設備旋轉的通知。

當我們關注的東西和布局相關而不是純粹設備旋轉,我們使用上面的代碼作為實現方案比較適合。

2.注冊UIDeviceOrientationDidChangeNotification通知(舉例:我們同樣在一個viewcontroller類的viewdidload中注冊該通知),示例代碼如下:

 

  [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

 

- (void)orientChange:(NSNotification *)noti

{

    

    NSDictionary* ntfDict = [noti userInfo];

    

    UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;

    /*

     UIDeviceOrientationUnknown,

     UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom

     UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top

     UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right

     UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left

     UIDeviceOrientationFaceUp,              // Device oriented flat, face up

     UIDeviceOrientationFaceDown             // Device oriented flat, face down   */

    

           switch (orient)

        {

            case UIDeviceOrientationPortrait:

                

                break;

            case UIDeviceOrientationLandscapeLeft:

    

                

                break;

            case UIDeviceOrientationPortraitUpsideDown:

 

          

                break;

            case UIDeviceOrientationLandscapeRight:

        

           

                break;

                

            default:

                break;

        }

}

注意到這種方式里面的方向還包括朝上或者朝下,很容易看出這個完全是根據設備自身的物理方向得來的,當我們關注的只是物理朝向時,我們通常需要注冊該通知來解決問題(另外還有一個加速計的api,可以實現類似的功能,該api較底層,在上面兩個方法能夠解決問題的情況下建議不要用,使用不當性能損耗非常大)。
 ==================================================
 
最近在ipad上面調試程序的時候 ,雖然  去掉了 屏幕旋轉的那倆鈎鈎,但是在ipadmini 上 還是 會  旋轉,很奇怪,ipad系統是 ios12 用 xcode10開發的程序。
 
 
解決方案
 

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
參考鏈接 https://blog.csdn.net/nadeal/article/details/79542682

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM