iOS之横竖屏小结
2016-01-25 16:21
441 查看
需求:项目要求是只支持竖屏的,但是视屏播放需要支持左右横屏。
首先,工程文件 -> General -> Device Orientation
选择 Portrait(正常竖屏)、Landscape Left (左横屏)、Landscape Right(右横屏);
如果在创建项目时,选择了 Devices:Universal,那么打开 info.plist 文件应该可以看到:
这里是配置横竖屏信息的地方,如果有不合适的地方可以进行调整。
然后,找到项目的根控制器,设置不支持屏幕旋转,并设置默认方向为正常反向。设置后会发现,凡是添加到根控制器的视图控制器都不再支持左右横屏。
方案一:
其中,第一个方法可以省略,因为在iOS 6.0后这个方法就被废弃了:
3、然后,实现MPMoviePlayerViewController的子类,并在 .m 文件中实现以下方法,就能实现需求了。
方案二:
以导航控制器为跟控制器为例:
这是网络上可以查到的一般解决方法,用在iPhone上的APP上效果显著。不过,如果你用iPad运行项目后会发现,需求还没有完成。在iPad处于横屏的状态下运行APP,会发现应用里的view是横屏显示的。这时,根控制器默认是不支持屏幕旋转的,所以整个APP一直处于横屏状态。当然,实现的MPMoviePlayerViewController子类还是可以正常旋转的。所以,我们只要将应用在iPad横屏状态下,也能以正常竖屏方向进入应用就OK了!
在这里有两个可以实现强制竖屏的方法:
方案一:
这是通过调用私有方法实现强制竖屏,但是为了应对App Store的审核,最好使用这种方法规避一下:
方案二:通过判断状态栏来设置视图的transform属性。
以上这两种方法实现的效果都还理想,只是在遇到iOS 9后的iPad,就没有那么好用了。让应用在iPad横屏状态下以正常竖屏进入,除了这两个方案外,还有其他方案,例如:在info.plist文件中设置横竖屏信息。
这样设置后,应用在iPad中启动后一定是以正常竖屏方向进入的。而且,iPhone设备不会受到影响,还会按照之前的代码设置执行横竖屏的逻辑。
应用在iPad上运行时,所有view都只会支持竖屏方向,为了让视频播放的view能够支持横竖屏旋转,需要用到transform属性来模拟系统的横竖屏效果。
这里是建立在方案二的基础上实现的:
由于是针对视屏播放界面,所以没有考虑状态栏的问题。
至此,需求就彻底完成了。本文旨在快速解决横竖屏问题,而没有对相关问题进行深入挖掘。
推荐文档:
(IOS:屏幕旋转与Transform:/article/4991332.html)
(transform属性详解:/article/7083440.html)
(IOS设备旋转的内部处理流程以及一些【优化建议】:/article/4791667.html)
(iOS全局禁止横屏,但UIWebVIew全屏播放视频支,横屏:/article/5668345.html)
首先,工程文件 -> 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:/article/4991332.html)
(transform属性详解:/article/7083440.html)
(IOS设备旋转的内部处理流程以及一些【优化建议】:/article/4791667.html)
(iOS全局禁止横屏,但UIWebVIew全屏播放视频支,横屏:/article/5668345.html)
相关文章推荐
- iOS-小的知识点
- IOS 开发,调用打电话,发短信,打开网址
- iOS导航栏的translucent属性
- IOS-48-解决开发中自定义控件在不同型号手机上不能显示的问题 (区别在于iOS修改了weak、strong属性)
- iOS 使用View 为屏幕增加一个全屏的蒙层
- ios 设置屏幕方向的两种方法
- iOS更新学习
- iOS 自定义TabBarController
- ios使用3dD Touch Screen Short Cut
- 在iOS开发中使用FMDB
- iOS ViewController里代码结构的规定
- iOS:Xcode7下创建 .a静态库 和 .framework静态库
- 【同行说技术】iOS从小白到大神必读资料汇总(四)
- iOS--利用比例纯代码适配屏幕大小
- IOS7 点击空白处隐藏键盘的几种方法
- 苹果开发者账号那些事儿(三)
- iOS 通知notification
- 在iOS7之后修改状态栏字体的颜色
- 浅析iOS开发者应用程序开发工具套件-iOS SDK
- ios高效开发-如何正确使用const,static,extern