iOS Rotation 判断

栏目: IOS · Android · 发布时间: 6年前

内容简介:通过通过注册

一、UIDeviceOrientation

UIDeviceOrientationUIDevice 中用于表示设备状态的枚举类型,其定义如下:

public enum UIDeviceOrientation : Int {

    case unknown

    case portrait // Device oriented vertically, home button on the bottom

    case portraitUpsideDown // Device oriented vertically, home button on the top

    case landscapeLeft // Device oriented horizontally, home button on the right

    case landscapeRight // Device oriented horizontally, home button on the left

    case faceUp // Device oriented flat, face up

    case faceDown // Device oriented flat, face down
}

通过 UIDevice.current.orientation 即可调用。

二、设备旋转通知

通过注册 UIDevice.orientationDidChangeNotification 通知,即可在设备形态切换的时候感知设备状态变化。

如果使用了 RxSwift,处理设备旋转通知的代码如下:

NotificationCenter.default.rx
    .notification(UIDevice.orientationDidChangeNotification)
    .subscribe(onNext: { _ in
        switch UIDevice.current.orientation {
        case .unknown:
        /// 未知
        case .portrait:
        /// 垂直
        case .portraitUpsideDown:
        /// 颠倒垂直
        case .landscapeLeft:
        /// 向左横屏
        case .landscapeRight:
        /// 向右横屏
        case .faceUp:
        /// 正面朝上
        case .faceDown:
        /// 背面朝上
        }
    }).disposed(by: bag)

三、扩展

UIDeviceOrientation 还包含一个 extension 扩展,提供了一系列布尔值属性,方便用户快速对设备状态作出判断:

extension UIDeviceOrientation {

    /// 是否处于竖屏状态
    public var isPortrait: Bool { get }

    /// 是否处于横屏状态
    public var isLandscape: Bool { get }

    /// 是否处于平放状态
    public var isFlat: Bool { get }

    /// 是否处于可判断UI旋转的状态
    public var isValidInterfaceOrientation: Bool { get }
}

其中值得一提的是 isValidInterfaceOrientation ,在官方文档中是这样描述的:

A Boolean value indicating whether the specified orientation is one of the portrait or landscape orientations.

通过 isValidInterfaceOrientation 可以快速判断当前设备是否处于 portrait , portraitUpsideDown , landscapeLeft , landscapeRight 中的任意一种状态,在处理设备旋转更新 UI 时非常有用。

四、小结

最大的坑点在于:

UIDevice.current.orientation中不仅包含横屏、竖屏状态,同时还包含了 FaceUp 和 FaceDown 状态。

在实际开发中,如果忽略了平放状态的话,很容易出现预期外的问题。

例如,假设需要在设备横竖屏切换时做 UI 和数据更新,我们很可能会写出类似下面的代码:

/// 处理设备旋转
func handleRotation()  {
    NotificationCenter.default.rx
        .notification(UIDevice.orientationDidChangeNotification)
        .subscribe(onNext: { _ in
            switch UIDevice.current.orientation {
            case .portrait, .portraitUpsideDown:
                /// 处理竖屏 UI 变更
                updateUI(isLandscape: false)
            case .landscapeLeft, .landscapeRight:
                /// 处理横屏 UI 变更
                updateUI(isLandscape: true)
            }
        }).disposed(by: bag)
}

写完之后在模拟器里一跑,完全没毛病。但是当在真机上测试时,问题就来了,具体表现为:

当用户将设备平放置于桌面上(甚至只需要将设备拿平一些),然后再将设备立起来的时候, UIDevice.current.orientation 会经历 faceUp/faceDown ==> portrait/landscape 的过程,同样会触发 updateUI 操作,进而导致不必要的 UI 渲染或数据更新,造成性能消耗。

因此,在处理设备旋转时,还需考虑设备的上一旋转状态。


以上所述就是小编给大家介绍的《iOS Rotation 判断》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

与孩子一起学编程

与孩子一起学编程

[美] 桑德Warren Sande、Carter Sande / 苏金国、姚曜 等 / 人民邮电出版社 / 2010-11 / 65.00元

一本老少咸宜的编程入门奇书!一册在手,你完全可以带着自己的孩子,跟随Sande父子组合在轻松的氛围中熟悉那些编程概念,如内存、循环、输入和输出、数据结构和图形用户界面等。这些知识一点儿也不高深,听起来备感亲切,书中言语幽默风趣而不失真义,让学习过程充满乐趣。细心的作者还配上了孩子们都喜欢的可爱漫画和经过运行测试的程序示例,教你用最易编写和最易理解的Python语言,写出你梦想中的游戏程序。 ......一起来看看 《与孩子一起学编程》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具