• 监听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

     
  • 相关阅读:
    Elastic Search的学习
    数据分析相关
    爬虫相关
    Git 知识总结
    运维开发
    Flask
    Linux入门
    MYSQL, REDIS 等数据库的介绍
    Django的学习之路
    逆向工具Frida 环境搭建
  • 原文地址:https://www.cnblogs.com/isItOk/p/5709304.html
Copyright © 2020-2023  润新知