作家
登录

代码处理iOS的横竖屏旋转

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

  •  
  •     UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), 
  •  
  •     UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), 
  •  
  •     UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), 
  •  
  •     UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), 
  •  
  •     UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), 
  •  
  • } __TVOS_PROHIBITED; 
  • 2)UIInterfaceOrientationMask的应用

    在ViewController可以重写- (UIInterfaceOrientationMask)supportedInterfaceOrientations办法返回类型,来决定UIViewController可以支撑哪些界面偏向。

    1. //支撑界面竖立 
    2.  
    3. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
    4.  
    5.     return UIInterfaceOrientationMaskPortrait; 
    6.  
    7.  

    总结:UIDeviceOrientation(设备偏向)和UIInterfaceOrientation(屏幕偏向)是两个不合的概念。前者代表了设备的一种状况,而后者是屏幕为了应对不合的设备状况,做出的用户界面上的响应。在iOS设备扭转时,由UIKit接收到扭转事宜,然后经由过程AppDelegate通知当前法度榜样的UIWindow对象,UIWindow对象通知它的rootViewController,如不雅该rootViewController支撑扭转后的屏幕偏向,完成扭转,不然不扭转;弹出的ViewController也是如斯处理。

    二、视图控制器中扭转偏向的设置

    比较惯例的办法有两种。

    办法1:在项目标General–>Deployment Info–>Device Orientation中,只勾选Portrait(竖屏)

    勾选Portrait.png

    办法2:Device Orientation默认设置,在Appdelegate中实现supportedInterfaceOrientationsForWindow:只返回UIInterfaceOrientationMaskPortraitt(竖屏)

    1. -  (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  {   
    2.  
    3.      return UIInterfaceOrientationMaskPortrait;   
    4.  
    5.  

    0、关于禁止横屏的操作(不建议)

    一、监听屏幕扭转偏向

    下面介绍若何让项目中的 视图控制器中扭转偏向的设置

    1、APP支撑多个偏向

    APP支撑多个偏向.png

    解释:如斯,APP支撑横屏和竖屏了,然则具体视图控制器支撑的页面偏向还须要进一步处理。因为不支撑竖屏倒置(Upside Down),即使设备高低倒置,经由过程API也不会获得设备、屏幕高低倒置偏向的。

    2、支撑ViewController屏幕偏向设置

    1)关键函数

    视图控制器支撑的界面偏向重要由以下三个函数控制

    1. //是否主动扭转,返回YES可以主动扭转,返回NO禁止扭转   
    2.  
    3. - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;   
    4.  
    5.   
    6.  
    7. //返回支撑的偏向   
    8.  
    9. - (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;   

        推荐阅读

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

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


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

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

    关键词: 探索发现

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

    网友点评
    自媒体专栏

    评论

    热度

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