您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 螢幕旋轉偵測通用做法:狀態欄方向 vs 裝置方向

編輯:iOS 開發綜合

某些情境下需要針對螢幕旋轉做特殊處理。iOS 提供了兩套通知可以監聽方向變化,但它們的語意完全不同,選錯會導致邏輯出錯。

方法一:UIApplicationDidChangeStatusBarOrientationNotification

這個通知監聽的是狀態欄(StatusBar)的方向,也就是「版面配置是否旋轉」,而非裝置本身的物理朝向。

// 在一個 view controller 的 viewDidLoad 中註冊
[[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(statusBarOrientationChange:)
                                            name:UIApplicationDidChangeStatusBarOrientationNotification
                                          object:nil];

- (void)statusBarOrientationChange:(NSNotification *)notification {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeRight) {   // home 鍵靠右
        // 在這裡處理
    }

    if (orientation == UIInterfaceOrientationLandscapeLeft) {    // home 鍵靠左
        // 在這裡處理
    }

    if (orientation == UIInterfaceOrientationPortrait) {
        // 在這裡處理
    }

    if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        // 在這裡處理
    }
}

什麼時候用它?當你關注的對象與「版面配置」相關時。例如某個 view 只在橫向時才顯示、或需要依 interface orientation 重排版面。因為它反映的是 interface 的旋轉狀態,若你的 App 鎖定了直向(Portrait lock),那即使使用者把裝置橫放,這個通知也不會觸發——這正是我們想要的行為。

方法二:UIDeviceOrientationDidChangeNotification

這個通知監聽的是裝置自身的物理方向,由加速計等感測器驅動,與 App 的版面是否旋轉無關。

// 同樣在 viewDidLoad 中註冊
[[NSNotificationCenter defaultCenter] addObserver:self
                                        selector:@selector(orientChange:)
                                            name:UIDeviceOrientationDidChangeNotification
                                          object:nil];

- (void)orientChange:(NSNotification *)noti {
    UIDeviceOrientation orient = [UIDevice currentDevice].orientation;

    /*
     UIDeviceOrientationUnknown
     UIDeviceOrientationPortrait,              // 直立,home 鍵在下方
     UIDeviceOrientationPortraitUpsideDown,    // 直立倒置,home 鍵在上方
     UIDeviceOrientationLandscapeLeft,         // 橫放,home 鍵在右側
     UIDeviceOrientationLandscapeRight,        // 橫放,home 鍵在左側
     UIDeviceOrientationFaceUp,                // 平放,螢幕朝上
     UIDeviceOrientationFaceDown               // 平放,螢幕朝下
     */

    switch (orient) {
        case UIDeviceOrientationPortrait:
            break;
        case UIDeviceOrientationLandscapeLeft:
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            break;
        case UIDeviceOrientationLandscapeRight:
            break;
        default:
            break;
    }
}

仔細看枚舉內容就會發現,這裡多出 FaceUp / FaceDown(螢幕朝上/朝下)兩種狀態。這清楚說明了它是裝置的物理朝向,與 interface 無關。

什麼時候用它?當你關心的只是「裝置怎麼擺」時。例如陀螺儀小遊戲、水平儀工具、拍照時判斷要旋轉照片,都必須用這個通知。使用前記得先呼叫 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];,並在 dealloc 中移除 observer 與呼叫 endGeneratingDeviceOrientationNotifications,否則會持續消耗電力。

兩者如何選擇

判斷標準只有一個問題:你關心的是「畫面怎麼轉」還是「手機怎麼擺」?

前者用 StatusBarOrientation,後者用 DeviceOrientation。混用是常見的 bug 來源——例如在鎖定直向的 App 裡用 DeviceOrientation 去調整版面,使用者把手機放平時版面就會莫名跑掉。

更現代的替代方案

1. 直接覆寫 size classes 與 trait 變化。自 iOS 8 起,更推薦的做法是實作 traitCollectionDidChange: 或直接遵循 viewWillTransitionToSize:withTransitionCoordinator:。前者處理水平/垂直尺寸類別的變化,後者能在旋轉時拿到目標尺寸並搭配 animator 做轉場動畫,兩者都比監聽通知更貼近 Auto Layout 的思維。

2. 用 viewWillLayoutSubviews 取代通知。如果只是要依新的 bounds 重新排版,其實不必監聽任何通知——系統在 layout 週期自然會用新的尺寸呼叫你,直接在這裡處理即可,程式碼最單純也最不容易出錯。

3. 加速計 API 請謹慎使用。Core Motion 的 API 確實能做到類似效果,但它屬於更底層的介面,若用上面兩種通知就能解決問題,就不要動用它。使用不當會造成明顯的效能損耗與電量浪費,特別是長時間開啟的情況。

另外補充一個已棄用的重點:statusBarOrientation 與 UIApplicationDidChangeStatusBarOrientationNotification 在 iOS 13 之後已被標記為 deprecated,官方建議改用 windowScene.interfaceOrientation。新專案請直接採用新的 API,只有維護舊程式碼時才需要沿用上述寫法。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved