【iOS】ScreenRotator - 屏幕旋轉工具類 隨時隨地改變/保持屏幕方向

ScreenRotator

屏幕旋轉工具類,能通過代碼隨時隨地改變/保持屏幕方向。

Feature:
    ? 可控制旋轉三個方向:
        - 豎屏:手機頭在上邊
        - 橫屏:手機頭在左邊
        - 橫屏:手機頭在右邊
    ? 可控制是否隨手機擺動自動改變屏幕方向;
    ? 適配iOS16;
    ? 兼容 OC & Swift & SwiftUI;
    ? API簡單易用。

Demo地址

使用效果

隨時隨地改變/保持屏幕方向
`push`或`present`一個跟當前方向不一樣的新頁面
視頻的橫豎屏切換

使用前提

  1. 讓單例ScreenRotator.shared全局控制屏幕方向,首先得在AppDelegate中重寫:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return ScreenRotator.shared.orientationMask
}
  1. 不需要再重寫ViewControllersupportedInterfaceOrientationsshouldAutorotate

  2. 如需獲取屏幕實時尺寸,在對應ViewController中重寫:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    // ??????:豎屏 --> 橫屏
    
    // 當屏幕發生旋轉時,系統會自動觸發該函數,`size`為【旋轉之后】的屏幕尺寸
    print("size \(size)") // --- (926.0, 428.0)
    // 或者通過`UIScreen`也能獲取【旋轉之后】的屏幕尺寸
    print("mainScreen \(UIScreen.main.bounds.size)") // --- (926.0, 428.0)

    // ?? 注意:如果想通過`self.xxx`去獲取屏幕相關的信息(如`self.view.frame`),【此時】獲取的尺寸還是【旋轉之前】的尺寸
    print("----------- 屏幕即將旋轉 -----------")
    print("view.size \(view.frame.size)") // - (428.0, 926.0)
    print("window.size \(view.window?.size ?? .zero)") // - (428.0, 926.0)
    print("window.safeAreaInsets \(view.window?.safeAreaInsets ?? .zero)") // - UIEdgeInsets(top: 47.0, left: 0.0, bottom: 34.0, right: 0.0)
    // ?? 想要獲取【旋轉之后】的屏幕信息,需要到`Runloop`的下一個循環才能獲取
    DispatchQueue.main.async {
        print("----------- 屏幕已經旋轉 -----------")
        print("view.size \(self.view.frame.size)") // - (926.0, 428.0)
        print("window.size \(self.view.window?.size ?? .zero)") // - (926.0, 428.0)
        print("window.safeAreaInsets \(self.view.window?.safeAreaInsets ?? .zero)") // - UIEdgeInsets(top: 0.0, left: 47.0, bottom: 21.0, right: 47.0)
        print("==================================")
    }
}
  1. 如需監聽屏幕的旋轉,不用再監聽UIDevice.orientationDidChangeNotification通知,而是監聽該工具類提供的ScreenRotator.orientationDidChangeNotification通知。或者通過閉包的形式實現監聽:
ScreenRotator.shard.orientationMaskDidChange = { orientationMask in 
    // 更新`FunnyButton`所屬`window`的方向
    FunnyButton.orientationMask = orientationMask
}

API

全局使用單例ScreenRotator.shared調用:

  1. 旋轉至目標方向
func rotation(to orientation: Orientation)
  1. 旋轉至豎屏
func rotationToPortrait()
  1. 旋轉至橫屏(如果鎖定了屏幕,則轉向手機頭在左邊)
func rotationToLandscape()
  1. 旋轉至橫屏(手機頭在左邊)
func rotationToLandscapeLeft()
  1. 旋轉至橫屏(手機頭在右邊)
func rotationToLandscapeRight()
  1. 橫豎屏切換
func toggleOrientation()
  1. 是否正在豎屏
var isPortrait: Bool
  1. 當前屏幕方向(ScreenRotator.Orientation)
var orientation: Orientation
  1. 是否鎖定屏幕方向(當控制中心禁止了豎屏鎖定,為true則不會【隨手機擺動自動改變】屏幕方向)
var isLockOrientationWhenDeviceOrientationDidChange = true 
// PS:即便鎖定了(`true`)也能通過該工具類去旋轉屏幕方向
  1. 是否鎖定橫屏方向(當控制中心禁止了豎屏鎖定,為true則【僅限橫屏的兩個方向會隨手機擺動自動改變】屏幕方向)
var isLockLandscapeWhenDeviceOrientationDidChange = false 
// PS:即便鎖定了(`true`)也能通過該工具類去旋轉屏幕方向
  1. 屏幕方向發生改變的回調
var orientationMaskDidChange: ((_ orientationMask: UIInterfaceOrientationMask) -> ())?
  1. 鎖定屏幕方向發生改變的回調
var lockOrientationWhenDeviceOrientationDidChange: ((_ isLock: Bool) -> ())?
  1. 鎖定橫屏方向發生改變的回調
var lockLandscapeWhenDeviceOrientationDidChange: ((_ isLock: Bool) -> ())?

可監聽的通知

  1. 屏幕方向發生改變的通知:
  • ScreenRotator.orientationDidChangeNotification
    • object: orientationMask(UIInterfaceOrientationMask)
  1. 鎖定屏幕方向發生改變的通知:
  • ScreenRotator.lockOrientationWhenDeviceOrientationDidChangeNotification
    • object: isLockOrientationWhenDeviceOrientationDidChange(Bool)
  1. 鎖定橫屏方向發生改變的通知:
  • ScreenRotator.lockLandscapeWhenDeviceOrientationDidChangeNotification
    • object: isLockLandscapeWhenDeviceOrientationDidChange(Bool)

兼容 OC & SwiftUI

  • OC:使用專門用OC寫的JPScreenRotator,用法和ScreenRotator完全一致。

  • SwiftUI:可以通過ScreenRotatorState來更新狀態。

    • 具體使用可以參考Demo中的RotatorView

Tips

pushpresent一個跟當前方向不一樣的新頁面時,建議先旋轉,再延時至少0.1s才打開,否則新頁面的屏幕方向會錯亂。例如:

let testVC = UIViewController()
// 1.先旋轉
ScreenRotator.shared.rotation(to: .landscapeRight)
// 2.延時至少0.1s再打開
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
    if let navCtr = self.navigationController {
        navCtr.pushViewController(testVC, animated: true)
    } else {
        self.present(testVC, animated: true)
    }  
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容