一般的應用,只會支持豎屏正方向一個方向,支持多個屏幕方向的應用還是比較少的。
不過我在工作的項目中,跟這個屏幕方向接觸比較多,因為我們是一個有界面的 SDK,要讓接入方接入的,一開始做沒什么經(jīng)驗,考慮到接入方本身的屏幕方向可能是多種的,所以我們直接上來就支持四個方向,然后就是各種轉(zhuǎn)屏的問題,90度旋轉(zhuǎn)、180讀旋轉(zhuǎn)、270度旋轉(zhuǎn),測試手都快轉(zhuǎn)斷了。
后來覺的根本沒必要,浪費了很多時間在解決屏幕方向的問題上,后來就簡化到讓接入方直接設置支持某個方向了。
一般的應用不用搞的這么的復雜,只要支持一兩個屏幕方向就可以了。我也做一下跟屏幕方向有關的幾點總結,希望能幫到一些開發(fā)者!
系統(tǒng)屏幕方向枚舉
通過查看文檔,用于控制系統(tǒng)屏幕方向的枚舉如下:
// iOS 6 之前用于控制屏幕方向的枚舉typedef enum { UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft} UIInterfaceOrientation;// iOS 6 及之后版本用于控制屏幕方向的枚舉typedef enum { UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),} UIInterfaceOrientationMask;
可以發(fā)現(xiàn):
怎么控制屏幕方向
在 iOS 的應用中,有多種方式可以控制界面的屏幕方向,有全局的,有針對 UIWindow 中界面的控制,也有針對單個界面。
單個界面控制
iOS 6之前
在 iOS 6 之前,單個界面的屏幕方向控制,都使用 UIViewController 類中的這個方法:
// 是否支持旋轉(zhuǎn)到某個屏幕方向- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ return ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) | (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft));}
默認情況下,此方法只有參數(shù)為 UIInterfaceOrientationPortrait 時,返回值才為真,即默認只支持豎屏向上。上面的例子中,表示支持橫屏向右及橫屏向左兩個方向。
iOS 6及之后的版本
在 iOS 6 及之后的版本,單個界面的屏幕方向控制,要使用 UIViewController 在 iOS 6.0 中新增加的兩個方法:
// 是否支持轉(zhuǎn)屏- (BOOL)shouldAutorotate{ return YES;}// 支持的屏幕方向,此處可直接返回 UIInterfaceOrientationMask 類型// 也可以返回多個 UIInterfaceOrientationMask 取或運算后的值- (NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskLandscape;}
其中 - supportedInterfaceOrientations 方法在 iPad 中默認取值為 UIInterfaceOrientationMaskAll,即默認支持所有屏幕方向;而 iPhone 跟 iPod Touch 的默認取值為 UIInterfaceOrientationMaskAllButUpsideDown,即支持除豎屏向下以外的三個方向。
在設備屏幕旋轉(zhuǎn)時,系統(tǒng)會調(diào)用 - shouldAutorotate 方法檢查當前界面是否支持旋轉(zhuǎn),只有 - shouldAutorotate 返回 YES 的時候,- supportedInterfaceOrientations 方法才會被調(diào)用,以確定是否需要旋轉(zhuǎn)界面。
UIWindow中的界面控制(iOS 6及以上版本才有效)
在 iOS 6 中,UIApplicationDelegate 協(xié)議中添加了一個可以指定 UIWindow 中的界面的屏幕方向的方法:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ return UIInterfaceOrientationMaskLandscape;}
此方法的默認值為 Info.plist 中配置的 Supported interface orientations 項的值。
一般我們都不會創(chuàng)建其他的 UIWindow,所以通過這個方法,也可以達到全局控制。
全局控制
在應用的 Info.plist 文件中,有一個 Supported interface orientations 的配置,可以配置整個應用的屏幕方向,如下圖:
此配置其實跟工程中 Target 的 Summary 界面中的 Supported interface orientations 配置是一致的,修改任意一邊,另一個邊都會同步的修改。
并且,應用在啟動時,會使用 Info.plist 中的 Supported interface orientations 項中的第一個值作為啟動動畫的屏幕方向。按照此處截圖的取值,第一個取值為 Portrait(top home button),即豎屏反方向,所以此應用在啟動時,會使用豎屏反方向顯示啟動動畫。
多種控制共存的規(guī)則
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選