亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

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

2020-07-26 02:25:39
字體:
來源:轉載
供稿:網友

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

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

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

下面還會舉例說明。

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

第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 ,有興趣的朋友歡迎來探討。

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩欧美成人免费视频| 亚洲国产精品一区二区三区| 欧美洲成人男女午夜视频| 欧美激情女人20p| 欧美日韩xxxxx| 亚洲精选中文字幕| 啊v视频在线一区二区三区| 亚洲另类激情图| 国产免费一区二区三区在线能观看| 欧美激情影音先锋| 成人自拍性视频| 欧美老女人xx| 91干在线观看| 中文字幕国产亚洲| 亚洲男人av在线| 91午夜理伦私人影院| 色妞色视频一区二区三区四区| 国产视频亚洲精品| 免费97视频在线精品国自产拍| 欧美精品激情blacked18| 国产视频精品久久久| 国产亚洲精品久久久久久| 在线国产精品视频| 久久国产加勒比精品无码| 日韩久久精品电影| 4438全国成人免费| 亚洲电影av在线| 97在线看福利| 8050国产精品久久久久久| 97精品一区二区视频在线观看| 福利一区福利二区微拍刺激| 亚洲精品电影在线观看| 日韩美女av在线免费观看| 91亚洲人电影| 自拍视频国产精品| 成人黄色免费网站在线观看| 久久久久久av| 久久免费成人精品视频| 久久免费国产精品1| 国产精品对白刺激| 亚洲一区二区中文字幕| 亚洲欧美日本另类| 午夜精品久久久久久99热| 全球成人中文在线| 欧美一区二区三区艳史| 久久精品视频网站| 欧美亚洲日本黄色| 日韩免费中文字幕| 九九久久久久久久久激情| 亚洲欧洲第一视频| 欧美亚洲激情视频| 国产+人+亚洲| 91最新在线免费观看| 久久久久久亚洲精品中文字幕| 亚洲激情视频在线| 北条麻妃一区二区三区中文字幕| 国产精品91久久久久久| 日韩av综合中文字幕| 日韩国产精品亚洲а∨天堂免| 啪一啪鲁一鲁2019在线视频| 国产精品久久久久久久天堂| 国产精品主播视频| 国产欧美日韩免费看aⅴ视频| 51精品国产黑色丝袜高跟鞋| 九九热最新视频//这里只有精品| 91久久精品久久国产性色也91| 亚洲免费av网址| 亚洲黄色在线观看| 久久视频国产精品免费视频在线| 国产精品xxxxx| 欧美电影在线观看完整版| 大桥未久av一区二区三区| 亚洲丝袜一区在线| 国产精品美女久久久免费| 午夜剧场成人观在线视频免费观看| 这里只有精品久久| 精品国产一区二区三区久久久| 久久免费视频在线观看| 亚洲综合成人婷婷小说| 青青草精品毛片| 中文字幕av一区中文字幕天堂| 色妞色视频一区二区三区四区| 亚洲国产精品久久久| 亚洲人精选亚洲人成在线| 日韩av在线一区二区| 日本a级片电影一区二区| 国产精品久久久久久婷婷天堂| 亚洲激情 国产| 欧美一级成年大片在线观看| 国产精品爽爽爽爽爽爽在线观看| 国产精品久久久久久搜索| 久久久国产精彩视频美女艺术照福利| 国产一区二区三区18| 国产精品国产亚洲伊人久久| 色一情一乱一区二区| 久久人人爽国产| 欧美xxxx18国产| 国产在线高清精品| 亚洲欧美精品在线| 曰本色欧美视频在线| 最新的欧美黄色| 亲爱的老师9免费观看全集电视剧| 欧美日韩成人在线播放| 欧美丰满老妇厨房牲生活| 国产视频久久久| 欧美日韩加勒比精品一区| 精品香蕉在线观看视频一| 亚洲人成电影网| 久久久亚洲影院你懂的| 45www国产精品网站| 日韩欧美精品网址| 超碰日本道色综合久久综合| 国产精品久久在线观看| 国内精品在线一区| 国产精品一区专区欧美日韩| 91精品美女在线| 久久躁狠狠躁夜夜爽| 精品福利在线观看| 国产日韩在线免费| 欧美精品一区二区三区国产精品| 91精品久久久久久| 91成人在线观看国产| 欧美精品第一页在线播放| 亚洲黄色成人网| 日韩综合视频在线观看| 一本久久综合亚洲鲁鲁| 操人视频在线观看欧美| 欧美精品videossex性护士| 亚洲女人被黑人巨大进入al| 成人av番号网| 日韩精品极品在线观看播放免费视频| 一区二区三区回区在观看免费视频| 91美女高潮出水| 亚洲欧美日韩中文在线| 国产免费观看久久黄| 成人黄色生活片| 91精品国产免费久久久久久| 日本久久久久久久久久久| 国产精品一二三在线| 国语自产精品视频在线看一大j8| 久久亚洲精品国产亚洲老地址| 欧美国产精品va在线观看| 日韩在线视频一区| 亚洲精品之草原avav久久| 97高清免费视频| 日韩电影中文字幕在线| 国产精品流白浆视频| 国产精品成人va在线观看| 九九久久精品一区| 国产91在线视频| 一个人看的www欧美| 蜜臀久久99精品久久久久久宅男| 欧美在线性视频| 日韩精品小视频| 亚洲国产天堂久久综合| 国产成人精品电影久久久| 欧洲成人在线观看| 国产精品99久久99久久久二8| 国产精品白丝jk喷水视频一区| 久久久视频精品| 91精品国产91久久久| 福利视频第一区| 欧美又大又粗又长| 欧美激情久久久|