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