iOS开发-UI控件:屏幕方向那点事儿

文章转自: http://zhenby.com/blog/2013/08/20/talk-ios-orientation/

一般的应用,只会支持竖屏正方向一个方向,支持多个屏幕方向的应用还是比较少的。
不过我在工作的项目中,跟这个屏幕方向接触比较多,因为我们是一个有界面的 SDK,要让接入方接入的,一开始做没什么经验,考虑到接入方本身的屏幕方向可能是多种的,所以我们直接上来就支持四个方向,然后就是各种转屏的问题,90度旋转、180读旋转、270度旋转,测试手都快转断了。
后来觉的根本没必要,浪费了很多时间在解决屏幕方向的问题上,后来就简化到让接入方直接设置支持某个方向了。 

一般的应用不用搞的这么的复杂,只要支持一两个屏幕方向就可以了。我也做一下跟屏幕方向有关的几点总结,希望能帮到一些开发者! 

系统屏幕方向枚举

通过查看文档,用于控制系统屏幕方向的枚举如下:

// iOS 6 之前用于控制屏幕方向的枚举
typedef enum {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

// iOS 6 及之后版本用于控制屏幕方向的枚举
typedef enum {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
			   UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
						UIInterfaceOrientationMaskLandscapeRight),
} UIInterfaceOrientationMask;

可以发现:

  • iOS 6 及之后版本使用的 UIInterfaceOrientationMask 类型来控制屏幕屏幕方向,该类型也新增加了几个枚举取值,可用一个枚举取值来代表多个屏幕方向。 
  • 四个基本屏幕方向(上、下、左、右)中,UIInterfaceOrientationMask = (1 << UIInterfaceOrientation),所以,如果你的应用中需要动态的将 UIInterfaceOrientation 类型转换成 UIInterfaceOrientationMask 类型的话,只需做一下上面的转换即可,不需要通过 switch 来判断再转换。 

怎么控制屏幕方向

在 iOS 的应用中,有多种方式可以控制界面的屏幕方向,有全局的,有针对 UIWindow 中界面的控制,也有针对单个界面。 

单个界面控制

iOS 6之前

在 iOS 6 之前,单个界面的屏幕方向控制,都使用 UIViewController 类中的这个方法: 

// 是否支持旋转到某个屏幕方向
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) |
	            (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}

默认情况下,此方法只有参数为 UIInterfaceOrientationPortrait 时,返回值才为真,即默认只支持竖屏向上。上面的例子中,表示支持横屏向右及横屏向左两个方向。 

iOS 6及之后的版本

在 iOS 6 及之后的版本,单个界面的屏幕方向控制,要使用 UIViewController 在 iOS 6.0 中新增加的两个方法:

// 是否支持转屏
- (BOOL)shouldAutorotate
{
    return YES;
}

// 支持的屏幕方向,此处可直接返回 UIInterfaceOrientationMask 类型
// 也可以返回多个 UIInterfaceOrientationMask 取或运算后的值
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

其中 - supportedInterfaceOrientations 方法在 iPad 中默认取值为 UIInterfaceOrientationMaskAll,即默认支持所有屏幕方向;而 iPhone 跟 iPod Touch 的默认取值为 UIInterfaceOrientationMaskAllButUpsideDown,即支持除竖屏向下以外的三个方向。
在设备屏幕旋转时,系统会调用 - shouldAutorotate 方法检查当前界面是否支持旋转,只有 - shouldAutorotate 返回 YES 的时候,- supportedInterfaceOrientations 方法才会被调用,以确定是否需要旋转界面。 

UIWindow中的界面控制(iOS 6及以上版本才有效)

在 iOS 6 中,UIApplicationDelegate 协议中添加了一个可以指定 UIWindow 中的界面的屏幕方向的方法:

- (NSUInteger)application:(UIApplication *)application
        supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskLandscape;
}

此方法的默认值为 Info.plist 中配置的 Supported interface orientations 项的值。
一般我们都不会创建其他的 UIWindow,所以通过这个方法,也可以达到全局控制。 

全局控制

在应用的 Info.plist 文件中,有一个 Supported interface orientations 的配置,可以配置整个应用的屏幕方向,如下图:
Supported interface orientations

此配置其实跟工程中 Target 的 Summary 界面中的 Supported interface orientations 配置是一致的,修改任意一边,另一个边都会同步的修改。
Target Summary

并且,应用在启动时,会使用 Info.plist 中的 Supported interface orientations 项中的第一个值作为启动动画的屏幕方向。按照此处截图的取值,第一个取值为 Portrait(top home button),即竖屏反方向,所以此应用在启动时,会使用竖屏反方向显示启动动画。 

多种控制共存的规则

  • 一个界面最后支持的屏幕方向,是取 (全局控制 ∩ UIWindow 中的界面控制 ∩ 单个界面控制) 的交集,如果全局控制支持所有屏幕方向,UIWindow 中的界面控制支持横屏,当个界面中只是支持横屏向右,那么最后界面只会以横屏向右显示,并且不支持旋转到其他的方向。 
  • 如果以上三种控制支持的屏幕方向最后的交集为空,iOS 5 跟 iOS 6 的处理有点不同,在 iOS 6 下,甚至会直接抛出 UIApplicationInvalidInterfaceOrientationException 的异常,然后直接崩溃,所以还是要保持这三个值的交集为非空。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值