iOS----应用的旋转---Orientations
此博文主要针对IOS应用, 是屏幕旋转相关问题的一个总结. 主要内容有:
- IOS5,6,7不同版的适配.
- 强制旋转和自动旋转.
- QQ : 1101819159
- 邮箱: GeekiVan@aliyun.com
- 作者: Magic-Van.
- 时间: 2016-11-25
改变Orientation的三种途径
这里, 咱们主要理清一下: 到底有哪些设置可以改变屏幕旋转特性. 这样:
- 出现任何问题我们都可以从这几个途径中发现原因.
- 灵活应付产品经理的各种需求.
首先我们得知道:
- 当手机的重力感应打开的时候, 如果用户旋转手机, 系统会抛发
UIDeviceOrientationDidChangeNotification
事件. - 您可以分别设置
Application
和UIViewcontroller
支持的旋转方向.Application
的设置会影响整个App,UIViewcontroller
的设置仅仅会影响一个viewController
(IOS5和IOS6有所不同,下面会详细解释). - 当
UIKit
收到UIDeviceOrientationDidChangeNotification
事件的时候, 会根据Application
和UIViewcontroller
的设置, 如果双方都支持此方向, 则会自动屏幕旋转到这个方向. 更code的表达就是, 会对两个设置求与,得到可以支持的方向. 如果求与之后,没有任何可支持的方向, 则会抛发UIApplicationInvalidInterfaceOrientationException
异常.
Info.plist设置
在App的Info.plist里设置:
key | xcode name | Summary | avilable value |
---|---|---|---|
UIInterfaceOrientation | initial interface orientation | Specifies the initial orientation of the app’s user interface. | UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight |
UISupportedInterfaceOrientations | Supported interface orientations | Specifies the orientations that the app supports. | UIInterfaceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight |
在Info.plist中设置之后,这个app里所有的viewController
支持的自动旋转方向都只能是app支持的方向的子集.
UIViewController
IOS6 and above
supportedInterfaceOrientations
在IOS6及以上的版本中, 增添了方法UIViewController.supportedInterfaceOrientations
. 此方法返回当前viewController
支持的方向. 但是, 只有两种情况下此方法才会生效:
- 当前
viewController
是window
的rootViewController
. - 当前
viewController
是modal
模式的. 即, 此viewController
是被调用presentModalViewController
而显示出来的.
在以上两种情况中,UIViewController.supportedInterfaceOrientations
方法会作用于当前viewController
和所有childViewController
. 以上两种情况之外, UIKit
并不会理会你的supportedInterfaceOrientations
方法.
举个栗子:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
如果某个viewController
实现了以上方法. 则, 此viewController
就支持竖方向和左旋转方向. 此viewController
的所有childViewController
也同时支持这两个方向, 不多不少.
preferredInterfaceOrientationForPresentation
此方法也属于UIViewController
. 影响当前viewController
的初始显示方向. 此方法也仅有在当前viewController
是rootViewController
或者是modal
模式时才生效.
shouldAutorotate
此方法,用于设置当前viewController
是否支持自动旋转. 如果,你需要viewController
暂停自动旋转一小会儿. 那么可以通过这个方法来实现.同样的, 此方法也仅有在当前viewController
是rootViewController
或者是modal
模式时才生效.
IOS5 and before
在IOS5和以前的版本中, 每个viewController
都可以指定自己可自动旋转的方向.(这样不是挺好么?苹果那帮工程师为啥要搞成这样...).
每当UIkit
收到UIDeviceOrientationDidChangeNotification
消息的时候, 就会用以下方法询问当前显示的viewController
支不支持此方向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ((orientation == UIInterfaceOrientationPortrait) ||
(orientation == UIInterfaceOrientationLandscapeLeft))
return YES;
return NO;
}
特别要注意的是:你必须至少要对一个方向返回YES
.(为难系统总不会有啥好事儿,你懂得).
UIView.transform
最后一个方法是设置UIView
的transform
属性来强制旋转.
见下代码:
//设置statusBar
[[UIApplication sharedApplication] setStatusBarOrientation:orientation];
//计算旋转角度
float arch;
if (orientation == UIInterfaceOrientationLandscapeLeft)
arch = -M_PI_2;
else if (orientation == UIInterfaceOrientationLandscapeRight)
arch = M_PI_2;
else
arch = 0;
//对navigationController.view 进行强制旋转
self.navigationController.view.transform = CGAffineTransformMakeRotation(arch);
self.navigationController.view.bounds = UIInterfaceOrientationIsLandscape(orientation) ? CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_WIDTH) : initialBounds;
需要注意的是:
- 当然我们可以对当前
viewController
进行旋转, 对任何view
旋转都可以.但是, 你会发现navigationBar
还横在那里. 所以, 我们最好对一个占满全屏的view
进行旋转. 在这里我们旋转的对象是self.navigationController.view
, 当然self.window
也可以, help yourself~ - 我们需要显式的设置
bounds
.UIKit
并不知道你偷偷摸摸干了这些事情, 所以没法帮你自动设置.
如何应付产品经理的需求
有了以上三把武器, 我想基本可以应付BT产品经理所有的需求了. 但是这里还有一些小技巧.
直接锁死
(略)
随系统旋转
IOS5及之前
对于IOS5及之前的版本, 只要在对每个viewController
重写shouldAutorotateToInterfaceOrientation
方法, 即可方便的控制每个viewController
的方向.
IOS6及以后
对于IOS6及以后的版本, 如果想方便的单独控制每个viewController
的方向. 则可以使用这样:
对于非
modal
模式的viewController
:- 如果不是
rootViewController
,则重写supportedInterfaceOrientations
,preferredInterfaceOrientationForPresentation
以及shouldAutorotate
方法, 按照当前viewController
的需要返回响应的值. - 如果是
rootViewController
,则如下重写方法:
- 如果不是
-(NSUInteger)supportedInterfaceOrientations
{
return self.topMostViewController.supportedInterfaceOrientations;
}
-(BOOL)shouldAutorotate
{
return [self.topMostViewController shouldAutorotate];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topMostViewController preferredInterfaceOrientationForPresentation];
}
-(UIViewController*)topMostViewController
{
//找到当前正在显示的viewController并返回.
}
显而易见, 我们巧妙的绕开了UIKit
只调用rootViewController
的方法的规则. 把决定权交给了当前正在显示的viewController
.
- 对于
modal
模式的viewController
. 则按照需要重写supportedInterfaceOrientations
,preferredInterfaceOrientationForPresentation
以及shouldAutorotate
方法即可.
强制旋转
有时候, 需要不随系统旋转, 而是强制旋转到某一个角度. 最典型的场景就是视频播放器, 当点击了全屏按钮的时候, 需要横过来显示.
- 对于IOS5及以前的版本, 可以用下面的方法:
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 = UIInterfaceOrientationLandscapeRight;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
- 对于IOS6及以后的版本.
UIDevice.setOrientation
从隐藏变为移除.只能通过设置UIView.transform
的方法来实现.
iOS----应用的旋转---Orientations的更多相关文章
- iOS实现屏幕旋转
iOS实现屏幕旋转有两种方式 1. 应用本身支持 2. 手动旋转UIView (这种方式我没找到旋转 系统控件的方法 如:键盘.导航.UIAlertView) 如果你只是要把竖屏的播放器,做成支持横屏 ...
- iOS 图片裁剪 + 旋转
iOS 图片裁剪 + 旋转 之前分别介绍了图片裁剪和图片旋转方法 <iOS 图片裁剪方法> 地址:http://www.cnblogs.com/silence-cnblogs/p/6490 ...
- ios实现屏幕旋转的方法
1.屏蔽AppDelegate下面的屏幕旋转方法 #pragma mark - 屏幕旋转的 //- (UIInterfaceOrientationMask)application:(UIApplica ...
- 【iOS】屏幕旋转,屏幕自适应方向变化
1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b
-(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...
- (转)ios限制控制器旋转
iOS屏幕旋转控制 iOS屏幕旋转控制(iOS6之后) iOS6之前,子控制器只要覆盖父类的shouldAutorotateToInterfaceOrientation:方法就能单独控制某 ...
- js 前端图片压缩+ios图片角度旋转
step1:读取选择的图片,并转为base64: function ImgToBase64 (e, fn) { // 图片方向角 //fn为传入的方法函数,在图片操作完成之后执行 var Orient ...
- iOS拍照图片旋转的问题
很久之前,遇到了这种情况,iOS某端拍照上传到服务器,其他iOS端从服务器下载该照片展示,发现图片逆时针旋转了90度.当时百度了一下,找到一段代码修正image方向,问题解决了,但没有深入理解底层原理 ...
- ios 关于屏幕旋转和屏幕晃动
内置加速计是智能手机最酷的特性之一,ios可以通过这个小设备知道用户握持手机的方式,以及用户是否移动了手机,ios使用加速计处理自动旋转,并且许多游戏都是用它作为控制机制,它还可以用于检测摇动和其他突 ...
随机推荐
- tLinux 2.2下安装Mono 4.8
Tlinux2.2发行版基于CentOS 7.2.1511研发而成,内核版本与Tlinux2.0发行版保持完全一致,更加稳定,并保持对Tlinux2.0的完全兼容.Mono 4版本要求CentOS 7 ...
- Android 自定义 attr
好纠结,弄了一个下午老是报错如是总结一下安卓自定视图和自定义属性. (一)自定义属性 在Values文件下建立一个attrs.xml文件,attr的format可以参考:http://www.cnbl ...
- 修改eclipse皮肤
习惯了vim黑色背景的程序猿们想必用eclipse时会倍感的不适应吧,不过没关系,因为eclipse的皮肤是可以自己定制的! 下面是我电脑上的eclipse界面,看到这个是不是找回了vim的感觉呢? ...
- php报错 ----> Call to undefined function imagecreatetruecolor()
刚才在写验证码的时候,发现报错,然后排查分析了一下,原来是所用的php版本(PHP/5.3.13)没有开启此扩展功能. 进入php.ini 找到extension=php_gd2.dll ,将其前面的 ...
- 【云知道】究极秒杀Loadrunner乱码
Loadrunner乱码一击必杀 之前有介绍一些简单的针对Loadrunner脚本或者调试输出内容中乱码的一些设置,但是并没能完全解决一些小伙伴的问题,因为那些设置实在能力有限,还是有很多做不到的事情 ...
- 转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38
转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38http://space.itpub. ...
- 机器指令翻译成 JavaScript —— No.6 深度优化
第一篇 中我们曾提到,JavaScript 最终还得经过浏览器来解析.因此可以把一些优化工作,交给脚本引擎来完成. 现代浏览器的优化能力确实很强,但是,运行时的优化终归是有限的.如果能在事先实现,则可 ...
- 【流量劫持】沉默中的狂怒 —— Cookie 大喷发
精简版:http://www.cnblogs.com/index-html/p/mitm-cookie-crack.html 前言 上一篇文章 讲解了如何借助前端技术,打造一个比 SSLStrip 更 ...
- Java的异步HttpClient
上篇提到了高性能处理的关键是异步,而我们当中许多人依旧在使用同步模式的HttpClient访问第三方Web资源,我认为原因之一是:异步的HttpClient诞生较晚,许多人不知道:另外也可能是大多数W ...
- 关于VS2015支持编译Linux程序的问题
现状 目前已经发布的VS2015中包括VS2015 Preview 以及 VS2015 CTP6,这两个版本均不支持直接编译C++代码为Linux程序,具体情况可以参考 Visual Studio 2 ...