热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS屏幕旋转与锁屏的示例代码

在做视频开发时遇到屏幕旋转问题,其中涉及到StatusBar、UINavigationController、UITabBarController、UIViewcontroller。在设备锁屏下的整

在做视频开发时遇到屏幕旋转问题,其中涉及到 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 <

由上可以发现:

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 ,有兴趣的朋友欢迎来探讨。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程笔记。


推荐阅读
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了如何找到并终止在8080端口上运行的进程的方法,通过使用终端命令lsof -i :8080可以获取在该端口上运行的所有进程的输出,并使用kill命令终止指定进程的运行。 ... [详细]
  • 本文详细介绍了PHP中与URL处理相关的三个函数:http_build_query、parse_str和查询字符串的解析。通过示例和语法说明,讲解了这些函数的使用方法和作用,帮助读者更好地理解和应用。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
author-avatar
phpxiaofei
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有