使用Swift開發一個貝樂虎啓蒙App - 橫豎屏切換

這是我參與8月更文挑戰的第7天,活動詳情查看:8月更文挑戰markdown

前言

接下來咱們將作視頻播放這塊,而視頻播放頁面是橫屏的,因此咱們先解決下橫豎屏切換問題。app

咱們先來看下關於方向的兩個枚舉和一個結構體:

UIDeviceOrientation:設備方向

這個枚舉值用來表示物理設備的方向ide

public enum UIDeviceOrientation : Int {
    case unknown = 0
    case portrait = 1 // Device oriented vertically, home button on the bottom
    case portraitUpsideDown = 2 // Device oriented vertically, home button on the top
    case landscapeLeft = 3 // Device oriented horizontally, home button on the right
    case landscapeRight = 4 // Device oriented horizontally, home button on the left
    case faceUp = 5 // Device oriented flat, face up
    case faceDown = 6 // Device oriented flat, face down
}
複製代碼

這個只能讀取,不能去設置,咱們能夠使用UIDevice.current.orientation來獲取設備方向,在代碼中監控這個方向的變化post

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    UIDevice.current.beginGeneratingDeviceOrientationNotifications()
    NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientation), name: UIDevice.orientationDidChangeNotification, object: nil)
    return true
}

@objc private func deviceOrientation() {
    switch UIDevice.current.orientation {
    case .portrait:
        debugPrint("portrait")
    case .portraitUpsideDown:
        debugPrint("portraitUpsideDown")
    case .landscapeLeft:
        debugPrint("landscapeLeft")
    case .landscapeRight:
        debugPrint("landscapeRight")
    case .faceUp:
        debugPrint("faceUp")
    case .faceDown:
        debugPrint("faceDown")
    default:
        debugPrint("unknown")
    }
}
複製代碼

UIInterfaceOrientation:頁面方向

這個是能夠設置的,注意: UIInterfaceOrientation.landscapeLeft等於UIDeviceOrientation.landscapeRight(反之亦然),這是由於向左旋轉設備須要向右旋轉內容spa

public enum UIInterfaceOrientation : Int {
    case unknown = 0
    case portrait = 1
    case portraitUpsideDown = 2
    case landscapeLeft = 4
    case landscapeRight = 3
}
複製代碼

UIInterfaceOrientationMask:頁面方向

這個和UIInterfaceOrientation有什麼區別呢?是一種爲了支持多種UIInterfaceOrientation而定義的debug

public struct UIInterfaceOrientationMask : OptionSet {
    public init(rawValue: UInt)
    public static var portrait: UIInterfaceOrientationMask { get }
    public static var landscapeLeft: UIInterfaceOrientationMask { get }
    public static var landscapeRight: UIInterfaceOrientationMask { get }
    public static var portraitUpsideDown: UIInterfaceOrientationMask { get }
    public static var landscape: UIInterfaceOrientationMask { get }
    public static var all: UIInterfaceOrientationMask { get }
    public static var allButUpsideDown: UIInterfaceOrientationMask { get }
}
複製代碼

設置頁面橫豎屏

一、AppDelegate裏面增長一個orientationMask屬性

//記錄當前界面橫豎屏旋轉方向
var orientationMask: UIInterfaceOrientationMask = .portrait {
    didSet {
        if orientationMask.contains(.portrait){
            //強制設置成豎屏
            UIDevice.current.setValue(NSNumber(value: UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
        } else {
            //強制設置成橫屏UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")

        }
        /// 刷新,若是不設置的話,當在橫屏狀態下鎖屏,而後在解鎖,在返回到上一頁會發現屏幕沒有更改過來
        UIViewController.attemptRotationToDeviceOrientation()
    }
}
複製代碼

二、在AppDelegatefunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask方法裏面返回orientationMask

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return orientationMask
}
複製代碼

三、在須要的設置屏幕旋轉的控制器裏面設置旋轉方向

3.一、在進入頁面的時候設置旋轉方向code

if let delegate = UIApplication.shared.delegate as? AppDelegate {
    delegate.orientationMask = .landscapeRight
}
複製代碼

3.二、在離開頁面的時候更改回來orm

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.orientationMask = .portrait
    }
}
複製代碼

如今屏幕旋轉搞定了,下一篇就開始作視頻播放了視頻

相關文章
相關標籤/搜索