一: ?序
二: ?基本知識(shí)點(diǎn)了解
三: ?屏幕旋轉(zhuǎn)流程
四: ?屏幕旋轉(zhuǎn)設(shè)置
四: ?結(jié)言
序:
這幾天項(xiàng)目要適配ipad,適配倒是沒出啥問題,沒想到橫豎屏幕切換出了問題,只好再看看查查看嘍@@好吧還是記錄一下吧,省的以后又忘了!
二: ?基本知識(shí)點(diǎn)了解
2.1屏幕旋轉(zhuǎn) orientation 分為兩種
1、device orientation
設(shè)備的物理方向
2、interface orientation
界面顯示的方向
三: ?屏幕旋轉(zhuǎn)流程
加速計(jì)是整個(gè)IOS屏幕旋轉(zhuǎn)的基礎(chǔ)。
依賴加速計(jì),設(shè)備才可以判斷出當(dāng)前的設(shè)備方向。
當(dāng)加速計(jì)檢測(cè)到方向變化的時(shí)候,會(huì)發(fā)出
UIDeviceOrientationDidChangeNotification 通知。
屏幕旋轉(zhuǎn)的流程如下:
1、加速計(jì)來識(shí)別設(shè)備的旋轉(zhuǎn)方向。
發(fā)送 UIDeviceOrientationDidChangeNotification 設(shè)備旋轉(zhuǎn)的通知。
2、app 接收到旋轉(zhuǎn)事件(通知事件)。
2、app 通過AppDelegate通知當(dāng)前程序的KeyWindow。
3、Window 會(huì)知會(huì)它的 rootViewController,判斷該view controller所支持的旋轉(zhuǎn)方向,完成旋轉(zhuǎn)。
4、如果存在 modal 的view controller的話,系統(tǒng)則會(huì)根據(jù) modal 的view controller,來判斷是否要進(jìn)行旋轉(zhuǎn)。
四: ?屏幕旋轉(zhuǎn)設(shè)置
4.1、如果項(xiàng)目整體不需要旋轉(zhuǎn),可以如下設(shè)置(不推薦)

4.2、如果項(xiàng)目需要個(gè)別頁(yè)面進(jìn)行橫豎屏切換,那就需要代碼控制
??4.2.1、 api接口使用
//是否支持屏幕旋轉(zhuǎn) (返回 NO 后面?zhèn)z方法不調(diào)用,后面只支持豎直方向)
override var shouldAutorotate: Bool {
return true
}
//支持屏幕旋轉(zhuǎn)的方向
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
//返回現(xiàn)在正在顯示的用戶界面方向
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
??4.2.1.1、 supportedInterfaceOrientations 使用說明:
此方法返回當(dāng)前viewController 支持的方向. 但是, 只有兩種情況下此方法才會(huì)生效:
1、當(dāng)前viewController是window的rootViewController.
2、當(dāng)前viewController是modal模式的. 即, 此viewController是被調(diào)用
presentModalViewController 而顯示出來的.
在以上兩種情況中,UIViewController.supportedInterfaceOrientations 方法會(huì)作用于前viewController和所有childViewController. 以上兩種情況之外, UIKit并不會(huì)理會(huì)你的supportedInterfaceOrientations 方法.
如果某個(gè)viewController實(shí)現(xiàn)了以上方法. 則, 此viewController就支持豎方向和左旋轉(zhuǎn)方向. 此viewController的所有childViewController也同時(shí)支持這兩個(gè)方向, 不多不少.
??4.2.1.2、 preferredInterfaceOrientationForPresentation 使用說明:
此方法也僅有在當(dāng)前viewController是rootViewController或者是modal模式時(shí)才生效.
??4.2.1.3、 shouldAutorotate 使用說明:
用于設(shè)置當(dāng)前viewController是否支持自動(dòng)旋轉(zhuǎn). 如果,你需要viewController
暫停自動(dòng)旋轉(zhuǎn)一小會(huì)兒. 那么可以通過這個(gè)方法來實(shí)現(xiàn).
同樣的, 此方法也僅有在當(dāng)前viewController是rootViewController或者是modal模式時(shí)才生效.
??4.2.1.4、 代碼設(shè)置:
由于全局性考慮,我們?cè)诖嗽O(shè)置UINavigationController UITabBarController 和 所有視圖控制器的父類BaseUIViewController,這三個(gè)類根據(jù)自己需求進(jìn)行自定義,如果不需要可以不設(shè)置, 同時(shí)在AppDelegate中也需要設(shè)置.
UINavigationController
在此類中加入如下代碼:
private extension UINavigationController {
var _visibleViewController: UIViewController? {
guard let viewController = self.topViewController else { return nil }
if let nc = viewController as? UINavigationController, let topViewController = nc.topViewController {
return topViewController
} else {
return viewController
}
}
}
override var shouldAutorotate: Bool {
return self._visibleViewController?.shouldAutorotate ?? false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self._visibleViewController?.supportedInterfaceOrientations ?? .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return self._visibleViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
}
UITabBarController
在此類中加入如下代碼:
private extension UITabBarController {
var _visibleViewController: UIViewController? {
guard let viewController = self.selectedViewController else { return nil }
if let nc = viewController as? UINavigationController, let topViewController = nc.topViewController {
return topViewController
} else {
return viewController
}
}
}
override var shouldAutorotate: Bool {
return self._visibleViewController?.shouldAutorotate ?? false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return self._visibleViewController?.supportedInterfaceOrientations ?? .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return self._visibleViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
}
UIViewController
在此類中加入如下代碼:
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
AppDelegate(必須設(shè)置)
在此類中加入如下代碼:
var blockRotation: Bool = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return blockRotation ? .all : .portrait
}
如果項(xiàng)目只需要橫屏或豎屏那么只需要在上面的類中直接設(shè)置死就可以了.
如果項(xiàng)目想某個(gè)頁(yè)面需要橫屏或豎屏請(qǐng)進(jìn)行如下操作:
在需要旋轉(zhuǎn)的頁(yè)面設(shè)置
let appDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
appDelegate.blockRotation = true
}
override func viewWillAppear(_ animated: Bool) {
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
appDelegate.blockRotation = false
let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
四: ?結(jié)言
以上內(nèi)容有很多不完整或錯(cuò)誤的地方,還望指正,在此也只是略做說明.