zoukankan      html  css  js  c++  java
  • IOS屏幕旋转

    加速计是整个IOS屏幕旋转的基础,依赖加速计。设备才干够推断出当前的设备方向,IOS系统共定义了下面七种设备方向:

    typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
        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
    };
       以及例如以下四种界面方向:

    typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
        UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
        UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
        UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
        UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
    };
     
    一、UIKit处理屏幕旋转的流程 

      当加速计检測到方向变化的时候,会发出 UIDeviceOrientationDidChangeNotification 通知,这样不论什么关心方向变化的view都能够通过注冊该通知,在设备方向变化的时候做出对应的响应。

    上一篇博客中,我们已经提到了在屏幕旋转的时候。UIKit帮助我们做了非常多事情。方便我们完毕屏幕旋转。



      UIKit的对应屏幕旋转的流程例如以下:

    1、设备旋转的时候,UIKit接收到旋转事件。



    2、UIKit通过AppDelegate通知当前程序的window。

    3、Window会知会它的rootViewController,推断该view controller所支持的旋转方向,完毕旋转。



    4、假设存在弹出的view controller的话,系统则会依据弹出的view controller。来推断是否要进行旋转。

     

    二、UIViewController实现屏幕旋转

      在响应设备旋转时。我们能够通过UIViewController的方法实现更细粒度的控制,当view controller接收到window传来的方向变化的时候。流程例如以下:

    1、首先推断当前viewController是否支持旋转到目标方向,假设支持的话进入流程2,否则此次旋转流程直接结束。

    2、调用 willRotateToInterfaceOrientation:duration: 方法。通知view controller将要旋转到目标方向。假设该viewController是一个container view controller的话。它会继续调用其content view controller的该方法。这个时候我们也能够临时将一些view隐藏掉。等旋转结束以后在现实出来。

    3、window调整显示的view controller的bounds,因为view controller的bounds发生变化,将会触发 viewWillLayoutSubviews 方法。这个时候self.interfaceOrientation和statusBarOrientation方向还是原来的方向。

    4、接着当前view controller的 willAnimateRotationToInterfaceOrientation:duration: 方法将会被调用。系统将会把该方法中运行的全部属性变化放到动animation block中。



    5、运行方向旋转的动画。

    6、最后调用 didRotateFromInterfaceOrientation: 方法,通知view controller旋转动画运行完成。这个时候我们能够将第二部隐藏的view再显示出来。

      整个响应步骤例如以下图所看到的:



      

      以上就是UIKit下一个完整的屏幕旋转流程。我们仅仅须要依照提示做出对应的处理就能够完美的支持屏幕旋转。



     

    三、注意事项和建议

      1)注意事项

      当我们的view controller隐藏的时候。设备方向也可能发生变化。

    比如view Controller A弹出一个全屏的view controller B的时候,因为A全然不可见,所以就接收不到屏幕旋转消息。

    这个时候假设屏幕方向发生变化,再dismiss B的时候,A的方向就会不对。

    我们能够通过在view controller A的viewWillAppear中更新方向来修正这个问题。

      2)屏幕旋转时的一些建议

    在旋转过程中。临时界面操作的响应。


    旋转前后,尽量当前显示的位置不变。


    对于view层级比較复杂的时候,为了提高效率在旋转開始前使用截图替换当前的view层级,旋转结束后再将原view层级替换回来。
    在旋转后最好强制reload tableview,保证在方向变化以后,新的row可以充满全屏。比如对于有些照片展示界面,竖屏仅仅显示一列。可是横屏的时候显示列表界面。这个时候一个界面就会显示很多其它的元素。此时reload内容就是非常有必要的。

  • 相关阅读:
    漫画 | 一台Linux服务器最多能支撑多少个TCP连接?
    frida hook pthread_create
    mac搜索so中的字符串
    字节字符串和数字互转
    twsited使用延迟项,并发量限制
    常见汇编指令和EFLAGS寄存器对应位的含义
    scrapy设置cookie的三种方式
    G1GC 读书笔记
    SLF4J 适配图
    支付宝和微信提现有办法免费了!
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7110517.html
Copyright © 2011-2022 走看看