iOS屏幕旋轉各類集錦(一)-單頁旋轉

如果你需要開啟旋轉
首先你需要開啟旋轉在info.plist點選(需要使用方向)

  • Portrait
  • Landscape Left
  • Landscape Right

或者在AppDelegate中實現代理方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortraitUpsideDown;
}

系統提供的樣式:無非是各種搭配一下和plist里的一樣

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
UIInterfaceOrientationMaskPortrait = (1 <<     UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
}
  • 首先讓我們來看看系統在程序的啟動過程UIKit處理屏幕旋轉的流程 
      當加速計檢測到方向變化的時候,會發出 UIDeviceOrientationDidChangeNotification 通知,這樣任何關心方向變化的view都可以通過注冊該通知,在設備方向變化的時候做出相應的響應。UIKit幫助我們做了很多事情,方便我們完成屏幕旋轉。UIKit的相應屏幕旋轉的流程如下:
    1、設備旋轉的時候,UIKit接收到旋轉事件。
    2、UIKit通過AppDelegate通知當前程序的window。
    3、Window會知會它的rootViewController,判斷該view controller所支持的旋轉方向,完成旋轉。
    4、如果存在彈出的view controller的話,系統則會根據彈出的view controller,來判斷是否要進行旋轉。

看了這么久你會發現,道理我都懂但是咋使用呢?

****NAV PushViewController 單獨設置某頁****

注 : 使用UINavigationController 父類實現不然子VC無效(原因就是在有UINavigationController的情況下rootViewController是UINavigationController)

父類實現方法,就可以讓某個單獨的VC獲得效果:

- (BOOL)shouldAutorotate  
{ 
 //也可以用topViewController判斷VC是否需要旋轉
return self.topViewController.shouldAutorotate;  
}  

- (NSUInteger)supportedInterfaceOrientations  
{   
    //也可以用topViewController判斷VC支持的方向
    return self.topViewController.supportedInterfaceOrientations;  
}

****Tabbat PushViewController 單獨設置某頁****

- (BOOL)shouldAutorotate {
 return [self.selectedViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
 return [self.selectedViewController supportedInterfaceOrientations];
}

tabbr+nav 需要同時設置才能生效

子類實現方法

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    //當前支持的旋轉類型
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate
{
    // 是否支持旋轉
    return YES;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    // 默認進去類型
  return   UIInterfaceOrientationPortrait;
}

PresentViewController 單獨設置某頁

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    // 是否支持旋轉
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    // 是否支持旋轉
    return YES;
}

iOS屏幕旋轉各類集錦(二)-單頁部分旋轉
***寫的比較粗糙demo附上https://github.com/bloodspasm/ScreenRotation ***

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,255評論 4 61
  • iOS屏幕旋轉學習筆記iOS開發中使用屏幕旋轉功能的相關方法 1、基本知識點解讀 了解屏幕旋轉首先需要區分兩種 o...
    Laughingg閱讀 13,566評論 13 39
  • 更好的閱讀體驗,請到個人博客閱讀: iOS中的系統轉場 請忽略標題,??,本文記錄的是對下圖所示的Kind, Pre...
    CaryaLiu閱讀 2,390評論 0 1
  • 三月有你 四月有你 五月你走了 六月是他自己 七月八月是雨季 九月十月連泡沫里帶著香氣 冬月煮酒臘月燒起了肉 一月...
    Fanderix閱讀 192評論 0 0
  • 藍色的碎片,紅色的碎片,紙上的馬賽克交織成了一個夢。 紅色的身體,并不代表著高傲;藍色的天空,承載起優雅的姿態。兔...
    漫慢書閱讀 240評論 0 3