作家
登录

代码处理iOS的横竖屏旋转

作者: 来源: 2017-07-25 13:05:41 阅读 我要评论

以上具体源码参考:QSRotationScreenDemo

在处理iOS反正屏时,经常会和UIDeviceOrientation、UIInterfaceOrientation和UIInterfaceOrientationMask这三个列举类型打交道,它们大年夜不合角度描述了屏幕扭转偏向。

1、UIDeviceOrientation:设备偏向

iOS的设备偏向是经由过程iOS的加快计来获取的。

1)iOS定义了以下七种设备偏向

  1. typedef NS_ENUM(NSInteger, UIDeviceOrientation) { 
  2.  
  3.     UIDeviceOrientationUnknown,                 // 未知偏向,可能是设备(屏幕)斜置 
  4.  
  5.     UIDeviceOrientationPortrait,                // 设备(屏幕)竖立 
  6.  
  7.     UIDeviceOrientationPortraitUpsideDown,      // 设备(屏幕)竖立,高低顛倒 
  8.  
  9.     UIDeviceOrientationLandscapeLeft,           // 设备(屏幕)向左横置 
  10.  
  11.     UIDeviceOrientationLandscapeRight,          // 设备(屏幕)向右橫置 
  12.  
  13.     UIDeviceOrientationFaceUp,                  // 设备(屏幕)朝上平躺 
  14.  
  15.     UIDeviceOrientationFaceDown                 // 设备(屏幕)手下平躺 
  16.  
  17. };  

解释:UIDeviceOrientation参考home键偏向,如:home偏向在右,设备(屏幕)偏向向左(UIDeviceOrientationLandscapeLeft)

2)攫取设备偏向

UIDevice单例代表当前的设备。大年夜这个单例中可以获得的信息设备,如设备偏向orientation。

  1. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; 

3)监听、处理和移除 设备偏向改变的通知

当设备偏向变更时刻,发出UIDeviceOrientationDidChangeNotification通知;注册监听该通知,可以针对不合的设备偏向处理视图展示。

解释:手机锁定竖屏后,UIDeviceOrientationDidChangeNotification通知就掉效了。

2、UIInterfaceOrientation:界面偏向

界面偏向是反竽暌功iOS中界面的偏向,它和Home按钮的偏向是一致的。

1)iOS定义了以下五种界面偏向

  1. typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { 
  2.  
  3.     UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,       //未知偏向 
  4.  
  5.     UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,               //界面竖立 
  6.  
  7.     UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,  //界面竖立,高低倒置 
  8.  
  9.     UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,   //界面朝左 
  10.  
  11.     UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft    //界面朝右 
  12.  
  13. } __TVOS_PROHIBITED;  

解释:大年夜定义可知,界面偏向和设别偏向有对应关系,如界面的竖直偏向就是 设备的竖直偏向:UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown

2)攫取界面偏向

  1. //开启和监听 设备扭转的通知(不开启的话,设备偏向一向是UIInterfaceOrientationUnknown) 
  2.  
     1/10    1 2 3 4 5 6 下一页 尾页

      推荐阅读

      2017,最受欢迎的 15 大 Python 库有哪些?

    近年来,Python 在数据科学行业扮演着越来越重要的角色。是以,我根据比来的应用体验,在本文中列出了对数据科学家、工程师们最有效的那些库。因为这些库都开源了,我们大年夜Github上惹人>>>详细阅读


    本文标题:代码处理iOS的横竖屏旋转

    地址:http://www.17bianji.com/lsqh/36388.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)