ios禁止屏幕旋轉的幾種方法

一般的應用,只會支持豎屏正方向一個方向,支持多個屏幕方向的應用還是比較少的。 
不過我在工作的項目中,跟這個屏幕方向接觸比較多,因爲我們是一個有界面的 SDK,要讓接入方接入的,一開始做沒什麼經驗,考慮到接入方本身的屏幕方向可能是多種的,所以我們直接上來就支持四個方向,然後就是各種轉屏的問題,90度旋轉、180讀旋轉、270度旋轉,測試手都快轉斷了。 
後來覺的根本沒必要,浪費了很多時間在解決屏幕方向的問題上,後來就簡化到讓接入方直接設置支持某個方向了。

一般的應用不用搞的這麼的複雜,只要支持一兩個屏幕方向就可以了。我也做一下跟屏幕方向有關的幾點總結,希望能幫到一些開發者!

系統屏幕方向枚舉

通過查看文檔,用於控制系統屏幕方向的枚舉如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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;

可以發現:

  • iOS 6 及之後版本使用的 UIInterfaceOrientationMask 類型來控制屏幕屏幕方向,該類型也新增加了幾個枚舉取值,可用一個枚舉取值來代表多個屏幕方向。
  • 四個基本屏幕方向(上、下、左、右)中,UIInterfaceOrientationMask = (1 << UIInterfaceOrientation),所以,如果你的應用中需要動態的將 UIInterfaceOrientation 類型轉換成 UIInterfaceOrientationMask 類型的話,只需做一下上面的轉換即可,不需要通過 switch 來判斷再轉換。

怎麼控制屏幕方向

在 iOS 的應用中,有多種方式可以控制界面的屏幕方向,有全局的,有針對 UIWindow 中界面的控制,也有針對單個界面。

單個界面控制

iOS 6之前

在 iOS 6 之前,單個界面的屏幕方向控制,都使用 UIViewController 類中的這個方法:

1
2
3
4
5
6
// 是否支持旋轉到某個屏幕方向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) |
              (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}

默認情況下,此方法只有參數爲 UIInterfaceOrientationPortrait 時,返回值才爲真,即默認只支持豎屏向上。上面的例子中,表示支持橫屏向右及橫屏向左兩個方向。

iOS 6及之後的版本

在 iOS 6 及之後的版本,單個界面的屏幕方向控制,要使用 UIViewController 在 iOS 6.0 中新增加的兩個方法:

1
2
3
4
5
6
7
8
9
10
11
12
// 是否支持轉屏
- (BOOL)shouldAutorotate
{
    return YES;
}

// 支持的屏幕方向,此處可直接返回 UIInterfaceOrientationMask 類型
// 也可以返回多個 UIInterfaceOrientationMask 取或運算後的值
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

其中 - supportedInterfaceOrientations 方法在 iPad 中默認取值爲 UIInterfaceOrientationMaskAll,即默認支持所有屏幕方向;而 iPhone 跟 iPod Touch 的默認取值爲 UIInterfaceOrientationMaskAllButUpsideDown,即支持除豎屏向下以外的三個方向。
在設備屏幕旋轉時,系統會調用 - shouldAutorotate 方法檢查當前界面是否支持旋轉,只有 - shouldAutorotate 返回 YES 的時候,- supportedInterfaceOrientations 方法纔會被調用,以確定是否需要旋轉界面。

UIWindow中的界面控制(iOS 6及以上版本纔有效)

在 iOS 6 中,UIApplicationDelegate 協議中添加了一個可以指定 UIWindow 中的界面的屏幕方向的方法:

1
2
3
4
5
- (NSUInteger)application:(UIApplication *)application
        supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskLandscape;
}

此方法的默認值爲 Info.plist 中配置的 Supported interface orientations 項的值。
一般我們都不會創建其他的 UIWindow,所以通過這個方法,也可以達到全局控制。

全局控制

在應用的 Info.plist 文件中,有一個 Supported interface orientations 的配置,可以配置整個應用的屏幕方向,如下圖:
Supported interface orientations

此配置其實跟工程中 Target 的 Summary 界面中的 Supported interface orientations 配置是一致的,修改任意一邊,另一個邊都會同步的修改。 Target Summary

並且,應用在啓動時,會使用 Info.plist 中的 Supported interface orientations 項中的第一個值作爲啓動動畫的屏幕方向。按照此處截圖的取值,第一個取值爲 Portrait(top home button),即豎屏反方向,所以此應用在啓動時,會使用豎屏反方向顯示啓動動畫。

多種控制共存的規則

  • 一個界面最後支持的屏幕方向,是取 (全局控制 ∩ UIWindow 中的界面控制 ∩ 單個界面控制) 的交集,如果全局控制支持所有屏幕方向,UIWindow 中的界面控制支持橫屏,當個界面中只是支持橫屏向右,那麼最後界面只會以橫屏向右顯示,並且不支持旋轉到其他的方向。
  • 如果以上三種控制支持的屏幕方向最後的交集爲空,iOS 5 跟 iOS 6 的處理有點不同,在 iOS 6 下,甚至會直接拋出 UIApplicationInvalidInterfaceOrientationException 的異常,然後直接崩潰,所以還是要保持這三個值的交集爲非空。
發佈了33 篇原創文章 · 獲贊 5 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章