ScreenRotator
屏幕旋轉(zhuǎn)工具類,能通過代碼隨時(shí)隨地改變/保持屏幕方向。
Feature:
? 可控制旋轉(zhuǎn)三個(gè)方向:
- 豎屏:手機(jī)頭在上邊
- 橫屏:手機(jī)頭在左邊
- 橫屏:手機(jī)頭在右邊
? 可控制是否隨手機(jī)擺動(dòng)自動(dòng)改變屏幕方向;
? 適配iOS16;
? 兼容 OC & Swift & SwiftUI;
? API簡(jiǎn)單易用。
使用效果

隨時(shí)隨地改變/保持屏幕方向

`push`或`present`一個(gè)跟當(dāng)前方向不一樣的新頁面

視頻的橫豎屏切換
使用前提
- 讓單例
ScreenRotator.shared全局控制屏幕方向,首先得在AppDelegate中重寫:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return ScreenRotator.shared.orientationMask
}
不需要再重寫
ViewController的supportedInterfaceOrientations和shouldAutorotate;如需獲取屏幕實(shí)時(shí)尺寸,在對(duì)應(yīng)
ViewController中重寫:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
// ??????:豎屏 --> 橫屏
// 當(dāng)屏幕發(fā)生旋轉(zhuǎn)時(shí),系統(tǒng)會(huì)自動(dòng)觸發(fā)該函數(shù),`size`為【旋轉(zhuǎn)之后】的屏幕尺寸
print("size \(size)") // --- (926.0, 428.0)
// 或者通過`UIScreen`也能獲取【旋轉(zhuǎn)之后】的屏幕尺寸
print("mainScreen \(UIScreen.main.bounds.size)") // --- (926.0, 428.0)
// ?? 注意:如果想通過`self.xxx`去獲取屏幕相關(guān)的信息(如`self.view.frame`),【此時(shí)】獲取的尺寸還是【旋轉(zhuǎn)之前】的尺寸
print("----------- 屏幕即將旋轉(zhuǎn) -----------")
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)
// ?? 想要獲取【旋轉(zhuǎn)之后】的屏幕信息,需要到`Runloop`的下一個(gè)循環(huán)才能獲取
DispatchQueue.main.async {
print("----------- 屏幕已經(jīng)旋轉(zhuǎn) -----------")
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("==================================")
}
}
- 如需監(jiān)聽屏幕的旋轉(zhuǎn),不用再監(jiān)聽
UIDevice.orientationDidChangeNotification通知,而是監(jiān)聽該工具類提供的ScreenRotator.orientationDidChangeNotification通知?;蛘咄ㄟ^閉包的形式實(shí)現(xiàn)監(jiān)聽:
ScreenRotator.shard.orientationMaskDidChange = { orientationMask in
// 更新`FunnyButton`所屬`window`的方向
FunnyButton.orientationMask = orientationMask
}
API
全局使用單例ScreenRotator.shared調(diào)用:
- 旋轉(zhuǎn)至目標(biāo)方向
func rotation(to orientation: Orientation)
- 旋轉(zhuǎn)至豎屏
func rotationToPortrait()
- 旋轉(zhuǎn)至橫屏(如果鎖定了屏幕,則轉(zhuǎn)向手機(jī)頭在左邊)
func rotationToLandscape()
- 旋轉(zhuǎn)至橫屏(手機(jī)頭在左邊)
func rotationToLandscapeLeft()
- 旋轉(zhuǎn)至橫屏(手機(jī)頭在右邊)
func rotationToLandscapeRight()
- 橫豎屏切換
func toggleOrientation()
- 是否正在豎屏
var isPortrait: Bool
- 當(dāng)前屏幕方向(ScreenRotator.Orientation)
var orientation: Orientation
- 是否鎖定屏幕方向(當(dāng)控制中心禁止了豎屏鎖定,為
true則不會(huì)【隨手機(jī)擺動(dòng)自動(dòng)改變】屏幕方向)
var isLockOrientationWhenDeviceOrientationDidChange = true
// PS:即便鎖定了(`true`)也能通過該工具類去旋轉(zhuǎn)屏幕方向
- 是否鎖定橫屏方向(當(dāng)控制中心禁止了豎屏鎖定,為
true則【僅限橫屏的兩個(gè)方向會(huì)隨手機(jī)擺動(dòng)自動(dòng)改變】屏幕方向)
var isLockLandscapeWhenDeviceOrientationDidChange = false
// PS:即便鎖定了(`true`)也能通過該工具類去旋轉(zhuǎn)屏幕方向
- 屏幕方向發(fā)生改變的回調(diào)
var orientationMaskDidChange: ((_ orientationMask: UIInterfaceOrientationMask) -> ())?
- 鎖定屏幕方向發(fā)生改變的回調(diào)
var lockOrientationWhenDeviceOrientationDidChange: ((_ isLock: Bool) -> ())?
- 鎖定橫屏方向發(fā)生改變的回調(diào)
var lockLandscapeWhenDeviceOrientationDidChange: ((_ isLock: Bool) -> ())?
可監(jiān)聽的通知
- 屏幕方向發(fā)生改變的通知:
-
ScreenRotator.orientationDidChangeNotification- object:
orientationMask(UIInterfaceOrientationMask)
- object:
- 鎖定屏幕方向發(fā)生改變的通知:
-
ScreenRotator.lockOrientationWhenDeviceOrientationDidChangeNotification- object:
isLockOrientationWhenDeviceOrientationDidChange(Bool)
- object:
- 鎖定橫屏方向發(fā)生改變的通知:
-
ScreenRotator.lockLandscapeWhenDeviceOrientationDidChangeNotification- object:
isLockLandscapeWhenDeviceOrientationDidChange(Bool)
- object:
兼容 OC & SwiftUI
OC:使用專門用OC寫的
JPScreenRotator,用法和ScreenRotator完全一致。-
SwiftUI:可以通過
ScreenRotatorState來更新狀態(tài)。- 具體使用可以參考Demo中的
RotatorView。
- 具體使用可以參考Demo中的
Tips
當(dāng)push或present一個(gè)跟當(dāng)前方向不一樣的新頁面時(shí),建議先旋轉(zhuǎn),再延時(shí)至少0.1s才打開,否則新頁面的屏幕方向會(huì)錯(cuò)亂。例如:
let testVC = UIViewController()
// 1.先旋轉(zhuǎn)
ScreenRotator.shared.rotation(to: .landscapeRight)
// 2.延時(shí)至少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)
}
}