UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例) 和 IOS6屏幕旋转详解
1.
UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例)
- 博客分类:
- IOS / Objective-C
UIDeviceOrientation 是机器硬件的当前旋转方向 这个你只能取值 不能设置
UIInterfaceOrientation 是你程序界面的当前旋转方向 这个可以设置
判断设备现在的方向:
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- //宣告一個UIDevice指標,並取得目前Device的狀況
- UIDevice *device = [UIDevice currentDevice] ;
- //取得當前Device的方向,來當作判斷敘述。(Device的方向型態為Integer)
- switch (device.orientation) {
- case UIDeviceOrientationFaceUp:
- NSLog(@"螢幕朝上平躺");
- break;
- case UIDeviceOrientationFaceDown:
- NSLog(@"螢幕朝下平躺");
- break;
- //系統無法判斷目前Device的方向,有可能是斜置
- case UIDeviceOrientationUnknown:
- NSLog(@"未知方向");
- break;
- case UIDeviceOrientationLandscapeLeft:
- NSLog(@"螢幕向左橫置");
- break;
- case UIDeviceOrientationLandscapeRight:
- NSLog(@"螢幕向右橫置");
- break;
- case UIDeviceOrientationPortrait:
- NSLog(@"螢幕直立");
- break;
- case UIDeviceOrientationPortraitUpsideDown:
- NSLog(@"螢幕直立,上下顛倒");
- break;
- default:
- NSLog(@"無法辨識");
- break;
- }
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向
- }
或者
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
- if (UIDeviceOrientationIsLandscape(deviceOrientation)) NSLog(@"横向");
- else if(UIDeviceOrientationIsPortrait(deviceOrientation)) NSLog(@"纵向");
- // // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向
- }
Portrait 表示 纵向,Landscape 表示 横向。
- typedef enum {
- 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
- } UIDeviceOrientation;
- typedef enum {
- UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
- UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
- } UIInterfaceOrientation;
- #define UIDeviceOrientationIsPortrait(orientation) ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)
- #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight)
上面是重要的源代码,已经解释的非常清楚。UIDeviceOrientationIsPortrait(orientation) 跟 ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown) 完全是一个意思。
2.IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
目录(?)[+]
转自:http://blog.csdn.net/cococoolwhj/article/details/8208991
概述:
在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。
但是iOS 6里屏幕旋转改变了很多,之前的 shouldAutorotateToInterfaceOrientation 被列为 DEPRECATED 方法,查看UIViewController.h文件也可以看到:
- // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序将使用如下2个方法来代替:
- - (BOOL)shouldAutorotate;
- - (NSUInteger)supportedInterfaceOrientations;
除了重写这个2个方法,IOS6里面要旋转还有一些需要注意的地方,下面会细述。另外还有一个硬性条件,需要在Info.plist文件里面添加程序支持的所有方向,可以通过以下2种方式添加
1.
2.
另外要兼容IOS6之前的系统,要保留原来的 shouldAutorotateToInterfaceOrientation 方法,还有那些 willRotateToInterfaceOrientation 等方法。
IOS6自动旋转设置:
- UIViewController *viewCtrl = [[UIViewController alloc] init];
- UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
- if ([window respondsToSelector:@selector(setRootViewController:)]) {
- self.window.rootViewController = navCtrl;
- } else {
- [self.window addSubview:navCtrl.view];
- }
如果需要设置viewCtrl的旋转,那么不能在UIViewController里面重写 shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面设置,又因为UINavigationController是系统控件,所以这里需要新建一个UINavigationController的子navigationController的子类,然后在里面实现shouldAutorotate和supportedInterfaceOrientations方法,比如:
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskAllButUpsideDown;
- }
- - (BOOL)shouldAutorotate{
- return YES;
- }
- UIViewController *viewCtrl2 = [[UIVewController alloc] init];
- [self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
- - (BOOL)shouldAutorotate
- {
- return self.topViewController.shouldAutorotate;
- }
- - (NSUInteger)supportedInterfaceOrientations
- {
- return self.topViewController.supportedInterfaceOrientations;
- }
全部调用self.topViewController,就是返回当前呈现出来的viewController里面的设置,然后在viewCtrl、viewCtrl2等等这些viewController里面重写 shouldAutorotate和 supportedInterfaceOrientations,以方便设置每个viewController的旋转
eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋转设置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包装过的viewCtrl3,那么就应在新包装的navController、tabbarController里面设置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面设置
手动旋转
手动旋转也有2种方式,一种是直接设置 UIDevice 的 orientation,但是这种方式不推荐,上传appStore有被拒的风险:
- if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
- [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
- }
第二种是假旋转,并没有改变 UIDevice 的 orientation,而是改变某个view的 transform,利用 CGAffineTransformMakeRotation 来达到目的,比如:
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2)
下面讲解采用第二种方式的各版本手动旋转:
思想是首先设置 statusBarOrientation,然后再改变某个view的方向跟 statusBarOrientation 一致!
IOS6手动旋转:
1. 那既然是旋转,最少也得有2个方向,那么还是少不了上面说的那个硬性条件,先在plist里面设置好所有可能需要旋转的方向。既然是手动旋转,那么就要关闭自动旋转:
- - (BOOL)shouldAutorotate{
- return NO;
- }
2.手动触发某个按钮,调用方法,这个方法的实现如下:
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
- self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
注意:
1. 只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的话,下面设置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情况下才管用!
IOS5、IOS4手动旋转:
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
- return (interfaceOrientation == [UIApplication sharedApplication].statusBarOrientation);
- }
2.手动触发某个按钮,调用方法,这个方法的实现如下:
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
- self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
注意:只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。
经验分享:
- -(NSUInteger)supportedInterfaceOrientations{
- return [UIApplication sharedApplication].statusBarOrientation;
- }
3.一般都不建议在程序里面直接调用 UIDeviceOrientation 的方向,而是用 UIInterfaceOrientation,他们之间是不同的!
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
看到吗,左右是反的!Xcode图形化设置方向也是以 UIInterfaceOrientation 为准,就是home按键所在的方向
参考:
http://blog.csdn.net/totogogo/article/details/8002173
http://stackoverflow.com/questions/13200220/how-to-change-keyboard-orientation-in-ios6
http://blog.csdn.net/yiyaaixuexi/article/details/8035014
UIDeviceOrientation 和 UIInterfaceOrientation 设备旋转的用法 (实例) 和 IOS6屏幕旋转详解相关推荐
- Jsoup解析HTML实例及文档方法详解
转载自 Jsoup解析HTML实例及文档方法详解 这篇文章主要介绍了Jsoup如何解析一个HTML文档.从文件加载文档.从URL加载Document等方法,对Jsoup常用方法做了详细讲解,最近提供 ...
- 解决C#程序只允许运行一个实例的几种方法详解
解决C#程序只允许运行一个实例的几种方法详解 参考文章: (1)解决C#程序只允许运行一个实例的几种方法详解 (2)https://www.cnblogs.com/randyzhuwei/p/5403 ...
- [深度学习概念]·实例分割模型Mask R-CNN详解
实例分割模型Mask R-CNN详解 基础深度学习的目标检测技术演进解析 本文转载地址 Mask R-CNN是ICCV 2017的best paper,彰显了机器学习计算机视觉领域在2017年的最新成 ...
- IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
转自 http://blog.csdn.net/zzfsuiye/article/details/8251060 概述: 在iOS6之前的版本中,通常使用 shouldAutorotateToInte ...
- java file 实例_Java File类的详解及简单实例
Java File类的详解及简单实例 1. File():构造函数,一般是依据文件所在的指定位置来创建文件对象. CanWrite():返回文件是否可写. CanRead():返回文件是否可读. Co ...
- ios6 屏幕旋转总结
1.在AppDelegate中添加如下代码: (1) self.window.rootViewController = self.iNavController; (2)// [self.win ...
- Oracle 数据库名、实例名、服务名详解
详解:数据库名.实例名.ORACLE_SID.数据库域名.全局数据库名.服务名 数据库名.实例名.数据库域名.全局数据库名.服务名,这是几个令很多初学者容易混淆的概念.相信很多初学者都被标题上这些概念 ...
- pythonmessage用法_django 消息框架 message使用详解
前言 在网页应用中,我们经常需要在处理完表单或其它类型的用户输入后,显示一个通知信息给用户. 对于这个需求,Django提供了基于Cookie或者会话的消息框架messages,无论是匿名用户还是认证 ...
- Android复习14【高级编程:推荐网址、抠图片上的某一角下来、Bitmap引起的OOM问题、三个绘图工具类详解、画线条、Canvas API详解(平移、旋转、缩放、倾斜)、矩阵详解】
目 录 推荐网址 抠图片上的某一角下来 8.2.2 Bitmap引起的OOM问题 8.3.1 三个绘图工具类详解 画线条 8.3.16 Canvas API详解(Part 1) 1.transla ...
最新文章
- vue-cli构建项目
- kubernetes node节点失效 调度
- Netweaver和CloudFoundry里的trace开关
- ms project 入门_Microsoft Project 2010入门
- sass编译css(转自阮一峰)
- Oracle使用技巧及PL/SQL Developer配置
- 【常识】常用RGB颜色对照表
- python语言在ansys的应用_Python语言在ANSYS的应用10讲-掌握SCDM脚本封装及ACT向导开发...
- Leetcode 1235. 规划兼职工作(DAY 73) ---- 动态规划学习期(上午去上高数课了 课下老师说上次旷课不扣平时分嘻嘻)
- 黑屏出现An operating system wasn't found.解决方案
- 转Genymetion
- mysql僵尸进程_僵尸Z进程和D进程
- 安卓图片处理Picasso的解析使用
- UE4插件BlueMan_VehicleAI使用
- poj2245枚举排列(DFS)
- APM 学习 6 --- ArduPilot 线程
- 找不到认证服务器 是否网卡选择错误,锐捷上网认证常见问题
- 大面积无线WIFI覆盖 H3C WX3010E(AC+PoE三层交换机)+ H3C WA2620E、WA4320无线AP +华为USG6310S防火墙
- SIM800C数据手册中没有提及的单链路TCP发送
- 用AI给娃定制绘本 #麻瓜+AI混合工作流试验 9
热门文章
- vue 获取公网IP和地理位置
- 软件工具系列:速记 IntelliJ IDEA快捷键,苹果电脑IDEA快捷键大全打印版本(一)
- java设计老鼠游戏_construct2制作小游戏——捉老鼠小游戏
- 不知道;不知道;我知道了;那我也…
- CorelDRAW Graphics Suite2022最新版新增功能及增强内容介绍
- 计算机剪切文件保存在哪里,电脑剪切的文件怎么恢复
- 产品上新需要注意什么 总结的思维导图分享给大家
- Ubuntu下录制gif动态图
- REXROTH力士乐比例溢流阀DBET6X/200G24K4V
- 计算机显卡驱动不起游戏,怎么看显卡驱动版本 解决玩游戏提示显卡驱动版本过低问题...