iOS 屏幕旋轉處理
在 iOS 設備上, 屏幕旋轉是個挺坑比的問題. 蘋果希望 app 對整個 app 做統(tǒng)一的旋轉處理, 而沒有提供單獨頁面的旋轉方案.
對于單獨頁面需要旋轉的情況下, 最好使用 Model 彈出 ViewController, 這樣 Application 不需要對當前 Application 的所有 Model 中的根控制器進行詢問.
Application 啟動初始化(根據 plist 來設置 OrientationMask)
-
詢問 AppDelegate 當前 App 所支持的 OrientationMask
AppDegete 中 application:supportedInterfaceOrientationsForWindow: 方法
-
如果是通過 Model 彈出的 ViewController, 則詢問當前 ModelViewController 所偏好的 Orientation, 否則執(zhí)行第4步:
ModelViewController 中的
preferredInterfaceOrientationForPresentation 方法 -
查看當前 App 屏幕上層 RootViewController 所支持的 Orientations
該 ViewController 中的
supportedInterfaceOrientations 方法
對比當前控制器所支持的方向, 并與 App 所支持的方向對比
相交有值則繼續(xù)進行, 相交無值則拋出異常 -
根據當前的重力方向, 在屏幕旋轉時詢問 RootViewController 是否需要旋轉
即控制器的
shouldAutorotate 方法, 返回 YES 則進行對應旋轉, 返回 NO 則不旋轉
但這種情況下, 執(zhí)行 Push Pop 操作 NavgationController 或者切換 TabbarController 時, 并不會通知系統(tǒng)進行回調(只有在 Model Present
操作或者 旋轉手機物理方向
時才會). 因此在執(zhí)行此類操作時, 需要手動強迫系統(tǒng)調用回調.
但實際上系統(tǒng)沒有提供這個方法, 目前僅能通過 KVC 設置系統(tǒng)的 Orientation 并告知系統(tǒng)去調用這些回調.
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];