国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院

首頁 > 系統 > iOS > 正文

iOS屏幕旋轉與鎖屏的示例代碼

2019-10-21 18:40:23
字體:
來源:轉載
供稿:網友

在做視頻開發時遇到屏幕旋轉問題,其中涉及到 StatusBar、 UINavigationController、UITabBarController 、UIViewcontroller 。

在設備鎖屏下的整體效果圖

iOS,屏幕旋轉,鎖屏,代碼

iOS-旋轉.gif

主要涉及以下4點:

  • 橫豎屏的旋轉
  • 屏幕旋轉相應改變視圖位置
  • 旋轉時狀態欄的隱藏與顯示
  • 鎖屏

1、橫豎屏旋轉

第1步:

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {//  NSLog(@"0000000---------%@",NSStringFromClass([[self topViewController] class]));//  if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"FirstViewController"]) {//    //橫屏//    return UIInterfaceOrientationMaskLandscapeRight;//  }//  //豎屏//  return UIInterfaceOrientationMaskPortrait;    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;  if(self.window.rootViewController){    //取出當前顯示的控制器    UIViewController *presentedViewController = [self topViewControllerWithRootViewController:self.window.rootViewController];    //按當前控制器支持的方向確定旋轉方向(將旋轉方向重新交給每個控制器自己控制)    NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);    orientations = [presentedViewController supportedInterfaceOrientations];  }  return orientations;}//獲取界面最上層的控制器//- (UIViewController*)topViewController {//  NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);//  return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];//}//一層一層的進行查找判斷- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {  NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);  if ([rootViewController isKindOfClass:[UITabBarController class]]) {        UITabBarController* tabBarController = (UITabBarController*)rootViewController;    NSLog(@"Tabbar:%@",NSStringFromClass([tabBarController.selectedViewController class]));    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];  } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {        UINavigationController* nav = (UINavigationController*)rootViewController;    NSLog(@"nav:%@",NSStringFromClass([nav.visibleViewController class]));    return [self topViewControllerWithRootViewController:nav.visibleViewController];  } else if (rootViewController.presentedViewController) {    NSLog(@"present:%@",NSStringFromClass([rootViewController.presentationController class]));    UIViewController* presentedViewController = rootViewController.presentedViewController;    return [self topViewControllerWithRootViewController:presentedViewController];  } else {    NSLog(@"root:%@",rootViewController);    return rootViewController;  }}

代碼中通過 -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 方法將控制器交給自己控制,該方法默認值為 Info.plist 中配置的 Supported interface orientations 項的值。

第2步:在各控制器設置支持的方向

//是否允許旋轉(默認允許)- (BOOL)shouldAutorotate {  return YES;}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{  //允許旋轉的方向  return UIInterfaceOrientationMaskAll;}

其中 - supportedInterfaceOrientations 方法在 iPad 中默認取值為 UIInterfaceOrientationMaskAll ,即默認支持所有屏幕方向;而 iPhone 跟 iPod Touch 的默認取值為 UIInterfaceOrientationMaskAllButUpsideDown ,即支持除豎屏向下以外的三個方向。

在設備屏幕旋轉時,系統會調用 - shouldAutorotate 方法檢查當前界面是否支持旋轉,只有 - shouldAutorotate 返回 YES 的時候, - supportedInterfaceOrientations 方法才會被調用,以確定是否需要旋轉界面。

這個是 TabbarController 中設置的,它會影響關聯的 UIViewController 的支持方向,需要在 UIViewController中進一步設置

//此方法來控制能否橫豎屏 控制鎖屏 - (UIInterfaceOrientationMask)supportedInterfaceOrientations {   NSLog(@"%s, line = %d",__FUNCTION__,__LINE__);   UIInterfaceOrientationMask inter;   if (_lockScreen) {     switch (_lockOrientation) {       case 1:         inter = UIInterfaceOrientationMaskPortrait;         break;       case 2:         inter = UIInterfaceOrientationMaskPortraitUpsideDown;         break;       case 3:         inter = UIInterfaceOrientationMaskLandscapeRight;         break;       case 4:         inter = UIInterfaceOrientationMaskLandscapeLeft;         break;       default:inter = UIInterfaceOrientationMaskAll;         break;     }   } else {     inter = UIInterfaceOrientationMaskAll;   }   //支持全部方向   return inter; }

第3步:強制轉換控制器方向

- (void)setInterOrientation:(UIInterfaceOrientation)orientation {      if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {     SEL selector       = NSSelectorFromString(@"setOrientation:");     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];     [invocation setSelector:selector];     [invocation setTarget:[UIDevice currentDevice]];     int val         = orientation;     // 從2開始是因為0 1 兩個參數已經被selector和target占用     [invocation setArgument:&val atIndex:2];     [invocation invoke];   } }

這樣就可以完成橫豎屏的切換。

2、屏幕旋轉相應改變視圖位置

這里先擴展 UIDeviceOrientation & UIInterfaceOrientation 的知識

UIDeviceOrientation 設備的物理方向

UIDeviceOrientation 即我們手持的移動設備的 Orientation ,是一個三圍空間,有六個方向,通過 [UIDevice currentDevice].orientation 獲取當前設備的方向。

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {  UIDeviceOrientationUnknown,  UIDeviceOrientationPortrait,        UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top 豎屏向下,即頭在下,Home 鍵在上  UIDeviceOrientationLandscapeLeft,    // Device oriented horizontally, home button on the right 橫屏頭在左,Home鍵在右  UIDeviceOrientationLandscapeRight,   // Device oriented horizontally, home button on the left 橫屏頭在右,Home鍵在左  UIDeviceOrientationFaceUp,       // Device oriented flat, face up  UIDeviceOrientationFaceDown       // Device oriented flat, face down} ;

UIInterfaceOrientation 界面的顯示方向

UIInterfaceOrientation 即我們看到的視圖的 Orientation ,可以理解為 statusBar 所在的方向,是一個二維空間,有四個方向, 通過 [UIApplication sharedApplication].statusBarOrientation 即狀態欄的方向獲取當前界面方向。

// Note that UIInterfaceOrientationLandscapeLeft is equal to  UIDeviceOrientationLandscapeRight (and vice versa).// This is because rotating the device to the left requires rotating the content to the right.typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {  UIInterfaceOrientationUnknown      = UIDeviceOrientationUnknown,  UIInterfaceOrientationPortrait      = UIDeviceOrientationPortrait,  UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,  UIInterfaceOrientationLandscapeLeft   = UIDeviceOrientationLandscapeRight,  UIInterfaceOrientationLandscapeRight   = UIDeviceOrientationLandscapeLeft}

UIInterfaceOrientationMask 支持的方向

// iOS 6 之后用于控制界面的枚舉值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),}

由上可以發現:

iOS 6 及之后版本使用的 UIInterfaceOrientationMask 類型來控制屏幕屏幕方向,該類型也新增加了幾個枚舉取值,可用一個枚舉取值來代表多個屏幕方向,使用起來更方便。

注意在 UIInterfaceOrientation 中有注釋

Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).

This is because rotating the device to the left requires rotating the content to the right,大意是界面的左轉相當于設備的右轉,如果設備向左轉時就需要內容(即界面)向右轉。即:

UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight 
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft

下面還會舉例說明。

其實 UIDeviceOrientation 與 UIInterfaceOrientation 是兩個互不相干的屬性,通常情況下會一起出現,在這里正好利用此特性在屏幕旋轉后進行重新布局。

第1步:監聽 UIDeviceOrientationDidChangeNotification 狀態

//監聽設備旋轉 改變 視圖 對應位置 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];//用來控制橫豎屏時調整視圖位置- (void)deviceOrientationDidChange{  [self isPortrait]; }

第2步:重新布局

if (_interOrientation == UIInterfaceOrientationPortrait || _interOrientation == UIInterfaceOrientationPortraitUpsideDown) {     self.top.constant = 145;     self.bottom.constant = 210;        } else if (_interOrientation == UIInterfaceOrientationLandscapeRight || _interOrientation == UIInterfaceOrientationLandscapeLeft) {     self.top.constant = 40;     self.bottom.constant = 50;   }

例如:豎屏轉橫屏

界面豎屏 UIInterfaceOrientationPortrait ->橫屏 UIInterfaceOrientationLandscapeRight ,設備方向 UIDeviceOrientationPortrait -> UIDeviceOrientationLandscapeLeft ,在設備發生變化這個過程觸發 UIDeviceOrientationDidChangeNotification 監聽,然后進行重新布局。

3、旋轉時狀態欄的隱藏與顯示

這里只記述旋轉時狀態欄的變化,由豎屏想橫屏變化時狀態欄會消失。

//在需要的`UIViewController`設置是否隱藏- (BOOL)prefersStatusBarHidden { NSLog(@"%s, line = %d",__FUNCTION__,__LINE__); return NO;}

4、鎖屏

鎖屏時,不管系統鎖屏是否關閉、Push 或 Present 返回后,界面依然保持不變。

第1步:設置鎖屏

- (IBAction)lockAction:(UIButton *)sender {   if (_lockScreen) {          _lockScreen = NO;     [sender setTitle:@"鎖定屏幕" forState:UIControlStateNormal];   } else {     _lockScreen = YES;          [sender setTitle:@"解開屏幕" forState:UIControlStateNormal];   }   _lockOrientation = _interOrientation; }

第2步:繞過強轉

- (void)interfaceOrientation:(UIInterfaceOrientation)orientation {      [self isPortrait];   //鎖屏情況下 不旋轉   if (!_lockScreen) {     [self setInterOrientation:orientation];   }

第3步:針對 Push 或 Present 返回后

- (void)viewWillAppear:(BOOL)animated {      if (_lockScreen) {     //記錄返回時的界面狀態     [self setInterOrientation:_lockOrientation];   } else {    [self isPortrait];   } }

5、 針對特定 UIViewController 方向的支持

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    if ([NSStringFromClass([[self topViewController] class]) isEqualToString:@"FirstViewController"]) {     //橫屏     return UIInterfaceOrientationMaskLandscapeRight;   }   //豎屏   return UIInterfaceOrientationMaskPortrait; }

最后的獻上 GitHub 代碼,還有2個小的 bug ,有興趣的朋友歡迎來探討。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
国产激情自拍_国产9色视频_丁香花在线电影小说观看 _久久久久国产精品嫩草影院
国产你懂的在线观看| 99re热在线观看| 影音先锋在线中文字幕| 中文字幕一区免费| 国产欧美日韩第一页| 精品网站www| 精品av中文字幕在线毛片| 国产激情视频在线| 亚洲成人在线播放| 就爱干草视频| 国产精品理人伦一区二区三区 | 国产国产人免费人成免费视频| 国产在线观看18| 国产视频中文字幕| www.操操| 国产成人午夜精品| 久久国产热视频| 在线一区观看| 自拍av在线| av片在线观看永久免费| 久久五月精品中文字幕| 国产精品视频一区二区三区麻豆| av中文资源在线| 国产精品扒开做爽爽爽的视频| 久久精品无码一区二区日韩av| 中文乱码字幕av网站| 国产极品视频| 狠狠操狠狠色| 啪啪免费视频一区| 操操操综合网| 在线视频观看你懂的| 99视频资源网| 免费看的毛片| av手机免费在线观看| 精品推荐蜜桃传媒| 精品一二三区视频| 精品免费视频一卡2卡三卡4卡不卡| 久久99精品久久久久久野外| 精品国产一区二区三区久久久狼牙 | 国产小视频在线高清播放| 亚洲视频在线网| 91最新在线| 国产福利在线免费观看| 国产黄网站在线观看| 99热最新网址| 国产秀色在线www免费观看| 99热在线观看免费| 国产精品视频一区麻豆| 国产图片综合| 国产区视频在线| 国产jizz| 亚洲第一区视频| sese一区| 国产性一级片| 精品成人免费自拍视频| 亚洲成年人视频| 久久久久久久久亚洲精品| 国产乱精品一区二区三区| 国产精品一区二区三区四区色| 国产在线高清| 国产黄色免费电影| 青青草免费在线观看| 国产嫩草在线视频| 中文字幕日本在线观看| 国产精品黄页网站在线播放免费| 国产大学生粉嫩无套流白浆| 一本大道香蕉久久| 最新黄网在线观看| 国产精品入口麻豆免费观看| 中文字幕av高清| 国产免费网址| 在线播放一区二区精品产| 成年人在线观看| av中文字幕在线看| 最新国产在线| 开心婷婷激情| 在线观看免费高清完整| 国产系列在线观看| 波多野结衣久久高清免费| 尤物网址在线观看| 日本卡一卡2卡3卡4精品卡网站| 自拍亚洲国产| 国产在线精品一区二区不卡| 在线免费观看黄色av| 国产日本韩国在线播放| 国产在线传媒| 国产裸舞福利在线视频合集| 日本不卡视频一区二区| 69免费视频| 国产欧美日韩第一页| 天天操人人干| 国产嫩草在线视频| 麻豆精品永久免费视频| 不卡av免费观看| 尤物在线精品视频| 国产精品视频一区二区免费不卡| 欧美精品另类| 九九热在线观看| 免费网站看黄yyy222| 亚洲综合在线不卡| 在线视频1区2区| 国产精品免费视频二三区| 天天操天天艹| 天天操人人干| 中文字幕视频在线免费| 中文字幕免费在线视频| 国产日产一区二区| 国产91在线视频蝌蚪| 91午夜在线| 在线中文字幕观看| 亚洲欧美久久婷婷爱综合一区天堂 | 精品一区二区三区在线成人| 国产对白叫床清晰在线播放| 国产精品白浆流出视频| 在线视频三区| 国产在线观看91| 国产精品扒开做爽爽爽的视频| 99热免费在线| 国产在线视频福利| 国产精品被窝福利一区| 亚洲欧美精选| 国产亚洲精品久久久久久移动网络 | 91这里只有精品| av手机天堂| 国产免费自拍视频| 精品入口麻豆传煤| 国产麻豆精品视频一区二区| 国产视频资源| 亚洲男人的天堂成人| 九九热在线观看视频| 国产成免费视频| 福利视频网址导航| 92久久精品| 尤物视频在线免费观看| sese一区| 在线观看中文字幕一区| 大香伊人中文字幕精品| 国产成人亚洲综合小说区| 麻豆精品视频入口| 精品久久久久一区二区三区| 久久亚洲天堂| a级在线观看| 伊人影院在线观看| 国产丝袜在线播放| 好看的中文字幕在线播放| 亚洲综合在线不卡| 在线中文资源天堂| 国产精品欧美色图| 中文字幕在线免费视频| 免费看的毛片| 美女国产在线| sm国产在线调教视频| av首页在线| 超碰91在线| 日本啊v在线| 天天插天天狠天天透| 精品成人一区二区三区免费视频| 中文字幕在线免费视频| 狠狠狠综合7777久夜色撩人| 日韩国产成人| 麻豆精品视频入口| 黄色毛片在线观看| 国产精品一区二区三区视频网站 | 亚洲一本大道| 亚洲91av| sese在线视频| 久草福利资源在线视频| 国产在线观看18| 日本最新在线视频| 国产精品久久久久久福利| 中文字幕免费中文| 国产网站麻豆精品视频| 国产精品免费视频一区一| 国产成人亚洲精品播放器下载| 国产不卡视频| 亚洲成人av高清| 国产乱视频在线观看| 国产婷婷视频在线| 99热99re6国产在线播放| 亚洲精品午夜级久久久久| sese在线视频| 国产深夜福利| av在线不卡播放| 国产精品一二三区视频| 国产精品yjizz视频网一二区 | 国产天堂在线播放视频| 国产高清视频在线| www.大网伊人| 国产香蕉免费精品视频| 在线国产中文字幕| 国产乱精品一区二区三区| 精精国产xxxx视频在线| 久久久久国产精品嫩草影院| 国产极品一区二区三区| 四虎一区二区三区| 国产精品久久麻豆| 国产精品亚洲色图| 欧美韩日国产| 国产日本韩国在线播放| www.久草.com|