需求:项目要求是只支持竖屏的,但是视屏播放需要支持左右横屏。
首先,工程文件 -> General -> Device Orientation
选择 Portrait(正常竖屏)、Landscape Left (左横屏)、Landscape Right(右横屏);
如果在创建项目时,选择了 Devices:Universal,那么打开 info.plist 文件应该可以看到:
这里是配置横竖屏信息的地方,如果有不合适的地方可以进行调整。
然后,找到项目的根控制器,设置不支持屏幕旋转,并设置默认方向为正常反向。设置后会发现,凡是添加到根控制器的视图控制器都不再支持左右横屏。
方案一:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}
/**重点:
根控制器的位置:
1.如果在UIViewController上,则在UIViewController对应的.m文件中加入三个函数即可。
2.如果在UITabBarController上,则在UITabBarController对应的.m文件中加入三个函数即可。
3.如果在UINavigationController上,则在UINavigationController对应的.m文件中加入三个函数即可。
(不按情况执行,问题无法解决)
*/
其中,第一个方法可以省略,因为在iOS 6.0后这个方法就被废弃了:
// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;
3、然后,实现MPMoviePlayerViewController的子类,并在 .m 文件中实现以下方法,就能实现需求了。
- (BOOL)shouldAutorotate
{
return Yes;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
方案二:
以导航控制器为跟控制器为例:
#pragma 横竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
这是网络上可以查到的一般解决方法,用在iPhone上的APP上效果显著。不过,如果你用iPad运行项目后会发现,需求还没有完成。在iPad处于横屏的状态下运行APP,会发现应用里的view是横屏显示的。这时,根控制器默认是不支持屏幕旋转的,所以整个APP一直处于横屏状态。当然,实现的MPMoviePlayerViewController子类还是可以正常旋转的。所以,我们只要将应用在iPad横屏状态下,也能以正常竖屏方向进入应用就OK了!
在这里有两个可以实现强制竖屏的方法:
方案一:
/**
*强制转成竖屏:
*/
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 = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
这是通过调用私有方法实现强制竖屏,但是为了应对App Store的审核,最好使用这种方法规避一下:
/**
* 强制竖屏
*/
NSArray * selectorNameCharacterArray = @[@"s",@"e",@"t",@"O",@"r",@"i",@"e",@"n",@"t",@"a",@"t",@"i",@"o",@"n",@":"];
NSString * selectorName = [selectorNameCharacterArray componentsJoinedByString:@""];
SEL selector = NSSelectorFromString(selectorName);
if ([[UIDevice currentDevice] respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
方案二:通过判断状态栏来设置视图的transform属性。
- (void)deviceOrientationDidChange: (NSNotification *)notification
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat startRotation = [[self valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
CGAffineTransform rotation;
switch (interfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 270.0 / 180.0);
break;
case UIInterfaceOrientationLandscapeRight:
rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 90.0 / 180.0);
break;
case UIInterfaceOrientationPortraitUpsideDown:
rotation = CGAffineTransformMakeRotation(-startRotation + M_PI * 180.0 / 180.0);
break;
default:
rotation = CGAffineTransformMakeRotation(-startRotation + 0.0);
break;
}
view.transform = rotation;
}
以上这两种方法实现的效果都还理想,只是在遇到iOS 9后的iPad,就没有那么好用了。让应用在iPad横屏状态下以正常竖屏进入,除了这两个方案外,还有其他方案,例如:在info.plist文件中设置横竖屏信息。
这样设置后,应用在iPad中启动后一定是以正常竖屏方向进入的。而且,iPhone设备不会受到影响,还会按照之前的代码设置执行横竖屏的逻辑。
应用在iPad上运行时,所有view都只会支持竖屏方向,为了让视频播放的view能够支持横竖屏旋转,需要用到transform属性来模拟系统的横竖屏效果。
这里是建立在方案二的基础上实现的:
#pragma 横竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (BOOL)shouldAutorotate
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
return YES;
}
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
if ([self.topViewController isKindOfClass:[MPMoviePlayerViewController class]] || [self.presentedViewController isKindOfClass:[MPMoviePlayerViewController class]]) {
if (iPad) {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation)){
switch (deviceOrientation) {
case 3:
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.presentedViewController.view.bounds = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
self.presentedViewController.view.transform = CGAffineTransformMakeRotation(-M_PI*1.5);
[UIView commitAnimations];
[USER_DEFAULTS setValue:@(deviceOrientation) forKey:@"deviceOrientation"];
}
break;
case 4:
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.presentedViewController.view.bounds = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
self.presentedViewController.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);
[UIView commitAnimations];
[USER_DEFAULTS setValue:@(deviceOrientation) forKey:@"deviceOrientation"];
}
break;
default:
break;
}
}else{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.presentedViewController.view.bounds = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.presentedViewController.view.transform = CGAffineTransformMakeRotation(0);
[USER_DEFAULTS setValue:@(deviceOrientation) forKey:@"deviceOrientation"];
[UIView commitAnimations];
}
}
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
由于是针对视屏播放界面,所以没有考虑状态栏的问题。
至此,需求就彻底完成了。本文旨在快速解决横竖屏问题,而没有对相关问题进行深入挖掘。
推荐文档:
(IOS:屏幕旋转与Transform:http://www.cnblogs.com/smileEvday/archive/2013/04/23/Rotate1.html)
(transform属性详解:http://www.cnblogs.com/iCocos/p/4639162.html)
(IOS设备旋转的内部处理流程以及一些【优化建议】:http://www.cnblogs.com/wengzilin/p/3258479.html)
(iOS全局禁止横屏,但UIWebVIew全屏播放视频支,横屏:http://www.cnblogs.com/piaojin/p/5089127.html)