IOS旋轉螢幕

來源:互聯網
上載者:User

標籤:依賴   cap   強制   屬性   window   提高   src   結束   ups   

加速計是整個IOS旋轉螢幕的基礎,依賴加速計。裝置才幹夠推斷出當前的裝置方向,IOS系統共定義了下面七種裝置方向:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};
   以及例如以下四種介面方向:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};
 
一、UIKit處理旋轉螢幕的流程 

  當加速計檢測到方向變化的時候,會發出 UIDeviceOrientationDidChangeNotification 通知,這樣不論什麼關心方向變化的view都能夠通過注冊該通知,在裝置方向變化的時候做出對應的響應。

上一篇部落格中,我們已經提到了在旋轉螢幕的時候。UIKit協助我們做了非常多事情。方便我們完畢旋轉螢幕。



  UIKit的對應旋轉螢幕的流程例如以下:

1、裝置旋轉的時候,UIKit接收到旋轉事件。



2、UIKit通過AppDelegate通知當前程式的window。

3、Window會知會它的rootViewController,推斷該view controller所支援的旋轉方向,完畢旋轉。



4、假設存在彈出的view controller的話,系統則會依據彈出的view controller。來推斷是否要進行旋轉。

 

二、UIViewController實現旋轉螢幕

  在響應裝置旋轉時。我們能夠通過UIViewController的方法實現更細粒度的控制,當view controller接收到window傳來的方向變化的時候。流程例如以下:

1、首先推斷當前viewController是否支援旋轉到目標方向,假設支援的話進入流程2,否則此次旋轉流程直接結束。

2、調用 willRotateToInterfaceOrientation:duration: 方法。通知view controller將要旋轉到目標方向。假設該viewController是一個container view controller的話。它會繼續調用其content view controller的該方法。這個時候我們也能夠臨時將一些view隱藏掉。等旋轉結束以後在現實出來。

3、window調整顯示的view controller的bounds,因為view controller的bounds發生變化,將會觸發 viewWillLayoutSubviews 方法。這個時候self.interfaceOrientation和statusBarOrientation方向還是原來的方向。

4、接著當前view controller的 willAnimateRotationToInterfaceOrientation:duration: 方法將會被調用。系統將會把該方法中啟動並執行全部屬性變化放到動animation block中。



5、運行方向旋轉的動畫。

6、最後調用 didRotateFromInterfaceOrientation: 方法,通知view controller旋轉動畫運行完成。這個時候我們能夠將第二部隱藏的view再顯示出來。

  整個響應步驟例如以所看到的:



  

  以上就是UIKit下一個完整的旋轉螢幕流程。我們僅僅須要依照提示做出對應的處理就能夠完美的支援旋轉螢幕。



 

三、注意事項和建議

  1)注意事項

  當我們的view controller隱藏的時候。裝置方向也可能發生變化。

比如view Controller A彈出一個全屏的view controller B的時候,因為A全然不可見,所以就接收不到旋轉螢幕訊息。

這個時候假設螢幕方向發生變化,再dismiss B的時候,A的方向就會不對。

我們能夠通過在view controller A的viewWillAppear中更新方向來修正這個問題。

  2)旋轉螢幕時的一些建議

在旋轉過程中。臨時介面操作的響應。


旋轉前後,盡量當前顯示的位置不變。


對於view層級比較複雜的時候,為了提高效率在旋轉開始前使用替換當前的view層級,旋轉結束後再將原view層級替換回來。
在旋轉後最好強制reload tableview,保證在方向變化以後,新的row可以充滿全屏。比如對於有些照片展示介面,豎屏僅僅顯示一列。可是橫屏的時候顯示列表介面。這個時候一個介面就會顯示很多其它的元素。此時reload內容就是非常有必要的。

IOS旋轉螢幕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.