iOS开发中屏幕旋转至一个方向后再手动设置屏幕旋转至该方向无效问题

转载自:http://blog.csdn.net/jinrui_w/article/details/77767893

场景描述

项目中用到的录制视频的功能,所以需要处理视频的横竖屏录制问题。说明:只有在录制视频页面才能旋转屏幕,其他页面都是竖屏显示。

首先,简单说下录制视频的逻辑。在录制视频前,屏幕是可以旋转的,已经开始录制后,禁止屏幕旋转。

我遇到的问题:

从一个页面(竖屏)跳转到视频录制页面,调整屏幕方向,横屏录制,开始录制后,屏幕不能旋转。此时,我想返回上一页面(竖屏)。那么问题来了,如果在返回上一页面前,将手机调整至竖屏显示方向(此时页面不可旋转,为横屏显示),再跳转至上一页面(竖屏),则手动设置的屏幕旋转无效。

说明:手动设置屏幕旋转的代码如下,

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}
  • 1
  • 2
  • 3
  • 4

问题分析

当调整手机的方向的时候,会触发两个方法shouldAutorotate和supportedInterfaceOrientations。 
如果按照上述步骤(我遇到的问题:)操作,最后手动设置屏幕旋转的代码未生效,未触发方法shouldAutorotate和supportedInterfaceOrientations。

解决办法

在手动设置屏幕旋转的代码前,设置先将屏幕旋转至其他方向,再旋转至最终的方向。

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}
  • 1
  • 2
  • 3
  • 4
  • 5

注意

关于屏幕旋转的方法,网上的资料很多。但是值得注意的是,如果你使用了UINavigationController或UITabBarController,需要做特殊处理。

猜你喜欢

转载自blog.csdn.net/lxlzy/article/details/79198105