屏幕旋转概述

来源:互联网 发布:中国知网cnki数据库 编辑:程序博客网 时间:2024/06/05 23:07

一、两种orientation

1、device orientation 设备的物理方向
在UIDevice类中有以下定义:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {    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};

2、interface orientation 界面显示的方向
在UIApplication中有以下定义

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),};

二、 相关方法

第一波方法:

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

说明:
第一个方法决定是否支持多方向旋转,如果返回NO则后面的两个方法都不会再被调用,而且只会支持默认的UIInterfaceOrientationMaskPortrait方向;
第二个方法直接返回所支持的旋转方向。该方法在IPad上的默认返回值是UIInterfaceOrientationMask,在IPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown ;
第三个方法返回最优先显示的屏幕方向。比如同时支持Portrait和Landscape放向,但是想优先显示Landscape方向,那么软件启动的时候就会显示Landscape方向,在手机切换选装方向的时候仍然可以在Portrait和Landscape之间切换。

第二波方法

1)// call this method when your return value from shouldAutorotateToInterfaceOrientation: changes// 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:+ (void)attemptRotationToDeviceOrientation NS_AVAILABLE_IOS(5_0);2)// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_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 的目的。

三、最终方向的决定

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

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

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

该方法默认值为 Info.plist 中配置的 Supported interface orientations 项的值。
iOS中通常只有一个 window,所以此处的控制也可以视为全局控制。

3.对于单个controller页面
1)如果是childViewController,则受限于rootViewController中shouldAutorotate和supportInterfaceOrientations支持的方向。
2)如果是ModalViewController,则受限于其自身的shouldAutorotate和supportedInterfaceOrientations支持的方向。即自身的这些方法会起作用。

4.以上所述3中控制规则的交集就是一个controller的最总支持方向;如果最终交集为空,则在IOS6之后,会抛出UIApplicationInvalidInterfaceOrientationException崩溃异常。

0 0