iOS屏幕旋转学习笔记

简介: <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,STheiti,微软雅黑,黑体,Arial,Tahoma,sans-serif,serif; font-si

一、两种orientation

了解屏幕旋转首先需要区分两种orientation

 

1、device orientation

设备的物理方向

 

2、interface orientation

界面显示的方向

 

iOS提供了在设备旋转时,界面显示发生相应适配的能力,以达到方便用户使用并提供最佳显示效果的目的。开发者需要指定应用支持的显示方向,并对界面显示做出对应的适配。由于界面适配的工作量相当大,目前国内的应用大都只支持默认的竖屏方向。

 

二、相关枚举定义

1、iOS 5和之前版本(后文均简称iOS5):

 
  1. typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { 
  2.     UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait, 
  3.     UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, 
  4.     UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight, 
  5.     UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft 
  6. }; 

 

2、iOS 6和之后版本(后文均简称iOS6)又新增了:

 
  1. typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { 
  2.     UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait), 
  3.     UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft), 
  4.     UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight), 
  5.     UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown), 
  6.     UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), 
  7.     UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown), 
  8.     UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight), 
  9. }; 

 

iOS 6使用 NS_OPTIONS 的方式重新定义了UIInterfaceOrientationMaskPortrait、UIInterfaceOrientationMaskLandscapeLeft、UIInterfaceOrientationMaskLandscapeRight、UIInterfaceOrientationMaskPortraitUpsideDown几种基础枚举,这就意味着能以组合的方式更加方便的使用这些枚举值。

 

三、相关方法

1、iOS 5中控制屏幕旋转的方法:

 
  1. // Applications should use supportedInterfaceOrientations and/or shouldAutorotate.. 
  2. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0); 

如果打算支持 toInterfaceOrientation 对应的方向就返回 YES,否则返回 NO。

 

2、iOS6中控制屏幕旋转相关方法:

 
  1. // New Autorotation support. 
  2. - (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0); 
  3. - (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0); 
  4. // Returns interface orientation masks. 
  5. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0); 

 

第一个方法决定是否支持多方向旋转屏,如果返回NO则后面的两个方法都不会再被调用,而且只会支持默认的UIInterfaceOrientationMaskPortrait方向;

 

第二个方法直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown,详情见官方Q&A文档;

 

第三个方法返回最优先显示的屏幕方向,比如同时支持Portrait和Landscape方向,但想优先显示Landscape方向,那软件启动的时候就会先显示Landscape,在手机切换旋转方向的时候仍然可以在Portrait和Landscape之间切换;

 

3、attemptRotationToDeviceOrientation方法

从iOS 5开始有了这个新方法:

 
  1. // call this method when your return value from shouldAutorotateToInterfaceOrientation: changes 
  2. // if the current interface orientation does not match the current device orientation, a rotation may occur provided all relevant view controllers now return YES from shouldAutorotateToInterfaceOrientation: 
  3. + (void)attemptRotationToDeviceOrientation NS_AVAILABLE_IOS(5_0); 

该方法的使用场景是 interface orientation和device orientation 不一致,但希望通过重新指定 interface orientation 的值,立即实现二者一致;如果这时只是更改了支持的 interface orientation 的值,没有调用attemptRotationToDeviceOrientation,那么下次 device orientation 变化的时候才会实现二者一致,关键点在于能不能立即实现。

 

举个例子:

假设当前的 interface orientation 只支持 Portrait,如果 device orientation 变成 Landscape,那么 interface orientation 仍然显示 Portrait;

 

如果这时我们希望 interface orientation 也变成和 device orientation 一致的 Landscape,以iOS 6 为例,需要先将 supportedInterfaceOrientations 的返回值改成Landscape,然后调用 attemptRotationToDeviceOrientation方法,系统会重新询问支持的 interface orientation,已达到立即更改当前 interface orientation 的目的。

 

四、如何决定interface orientation

1、全局控制

Info.plist文件中,有一个Supported interface orientations,可以配置整个应用的屏幕方向,此处为全局控制。

 

2、UIWindow

iOS6的UIApplicationDelegate提供了下述方法,能够指定 UIWindow 中的界面的屏幕方向:

 
  1. - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  NS_AVAILABLE_IOS(6_0); 

该方法默认值为 Info.plist 中配置的 Supported interface orientations 项的值。

 

iOS中通常只有一个 window,所以此处的控制也可以视为全局控制。

 

3、controller

只有以下两种情况:

当前controller是window的rootViewController

当前controller是modal模式的时,orientations相关方法才会起作用(才会被调用),当前controller及其所有的childViewController都在此作用范围内。

 

4、最终支持的屏幕方向

前面所述的3种控制规则的交集就是一个controller的最终支持的方向;

 

如果最终的交集为空,在iOS6以后会抛出UIApplicationInvalidInterfaceOrientationException崩溃异常。

 

四、强制屏幕旋转

如果interface和device方向不一样,想强制将interface旋转成device的方向,可以通过attemptRotationToDeviceOrientation实现,但是如果想将interface强制旋转成任一指定方向,该方式就无能为力了。

 

不过聪明的开发者们总能想到解决方式:

 

1、私有方法

 
  1. [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 

 

但是现在苹果已经将该方法私有化了,越狱开发的同学可以试试。

 

2、旋转view的transform

也可以通过旋转view的transform属性达到强制旋转屏幕方向的目的,但个人感觉这不是靠谱的思路,可能会带来某些诡异的问题。

 

3、主动触发 orientation 机制

要是能主动触发系统的 orientation 机制,调用 orientation 相关方法,使新设置的 orientation 值起作用就好了。这样只要提前设置好想要支持的 orientation,然后主动触发 orientation 机制,便能实现将 interface orientation旋转至任意方向的目的。

 

万能的stackoverflow上提供了一种主动触发的方式:

 

在iOS 4和iOS 6以后:

 
  1. UIViewController *vc = [[UIViewController alloc]init]; 
  2. [self presentModalViewController:vc animated:NO]; 
  3. [self dismissModalViewControllerAnimated:NO]; 
  4. [vc release]; 

 

iOS 5中:

 
  1. UIWindow *window = [[UIApplication sharedApplication] keyWindow]; 
  2. UIView *view = [window.subviews objectAtIndex:0]; 
  3. [view removeFromSuperview]; 
  4. [window addSubview:view]; 

 

这种方式会触发UIKit重新调用controller的orientation相关方法,以达到在device方向不变的情况下改变interface方向的目的。

 

虽然不优雅,但却能解决问题,凑合吧。。

 

PS:

话说iOS8中的屏幕旋转相关方法又变化了,表示适配起来很蛋疼。

目录
相关文章
|
7月前
|
iOS开发
iOS UIDevice & 屏幕旋转检测
iOS UIDevice & 屏幕旋转检测
30 0
|
自然语言处理 开发工具 Swift
移动用户反馈-iOS接入指南 | 学习笔记
快速学习移动用户反馈-iOS接入指南
221 0
移动用户反馈-iOS接入指南 | 学习笔记
|
安全 vr&ar 数据安全/隐私保护
iOS移动设备屏幕镜像电脑软件AirServer2023
AIrServer是一款ios投屏到mac的专用软件,可将iOS上的音频,视频,照片,幻灯片和镜像接收通过AIrPlay投射到Mac。AIrserver 7 mac版可以实现将手机上的媒体文件以及其他操作投射到电脑上进行操作。使用AIrServer,您现在可以从Mac,iOS,PC,
152 0
|
iOS开发 Perl
iOS 屏幕比例适配
iOS 屏幕比例适配
|
iOS开发
IOS锁定屏幕旋转
IOS锁定屏幕旋转
73 0
|
iOS开发
ios 锁定 屏幕旋转
ios 锁定 屏幕旋转
32 0
|
小程序 前端开发 Shell
接入 mPaaS 小程序并实现启动 iOS 版| 学习笔记
快速学习接入 mPaaS 小程序并实现启动 iOS 版。
561 0
接入 mPaaS 小程序并实现启动 iOS 版| 学习笔记
|
移动开发 小程序 前端开发
iOS 端自定义开发(二)| 学习笔记
快速学习 iOS 端自定义开发。
171 0
iOS 端自定义开发(二)| 学习笔记
|
移动开发 JSON 小程序
iOS 端自定义开发(一)| 学习笔记
快速学习 iOS 端自定义开发。
222 0
iOS 端自定义开发(一)| 学习笔记
|
小程序 IDE API
iOS 端预览和调试小程序| 学习笔记
快速学习 iOS 端预览和调试小程序。
203 0
iOS 端预览和调试小程序| 学习笔记