ios解密ios 横屏竖屏切换游戏

141被浏览13,914分享邀请回答8527 条评论分享收藏感谢收起3添加评论分享收藏感谢收起iOS之横屏那些事 - 简书
iOS之横屏那些事
github Demo
0. 横屏应用领域
视频播放、直播
展示内容较多且有较多细节的,VR、全景、摄影教学、股市数据等App
iOS6开始禁止使用setOrientation:私有方法,开发人员仅能通过project-&Device Orientation设置对应方向,然而对于大量采用frame形式组织页面布局的App来说,适配横屏简直就是灾难。
那么原先仅支持竖屏的App如何快速适配横屏呢?
根据需求,Device Orientation中新增设备支持方向
保持原先App结构,BasicViewController 默认支持Portrait,业务VC自行适配横屏逻辑
测试LandscapeViewController,dismiss or pop or push 各种形式,对于其他页面的影响,对StatusBar的影响
测试横屏模式冷、热启动App是否导致页面布局错乱,比如,横屏H5页面唤起App,plus横屏模式唤起App
如果全新的一个项目想要支持横竖屏&Universal App 需要符合autolayout,也可以用。
2. 方向类型
1. 方向类型:
Device Orientation
Interface Orientation
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown
= UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait
= UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft
= UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight
= UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
注意InterfaceLandscape与DeviceLandscape取反
2. 方向类型获取:
3. 如何支持横屏
1. 方法一:
transition, 通过选择View的形式达到横屏效果,判断依据:键盘,上拉快捷方式方向不一致。适合简单不需要键盘输入交互,比如简单的视频播放。
CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI_2);
CGFloat scale = 0.5;
transform = CGAffineTransformScale(transform, scale, scale);
self.xxView.transform =
[UIView animateWithDuration:[[UIApplication sharedApplication] statusBarOrientationAnimationDuration] animations:^{
self.xxView.transform = CGAffineTransformI
self.xxView.frame = fullScreenVC.view.
}completion:^(BOOL finished) {
2. 方法二:
通过私有API形式调用
SEL selector
= NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
这种形式可行,但是存在被拒审的风险,被否决掉!
3. 方法三:
如果是 TabBarController & NavigationController结构的,为了支持页面的横屏需要做如下改造,工程设置支持对应的方向,默认ViewController只支持竖屏模式,只需要presentViewController即可,缺点在于无法适用之前push的导航容器,如有需要可以自行创建:
TabBarController:
supportedInterfaceOrientations 需要向上抛到NavigationController的supportedInterfaceOrientations
shouldAutorotate也是同理
NavigationController
return self.topviewcontroller supportedInterfaceOrientations
return self.topViewController shouldAutorotate
viewController
默认不用设置方向,前提基于BasicVC,默认竖屏
有需要再去设置supportInterfaceOrientation & shouldAutorotate
注意事项:
shouldAutorotate 设置为NO后,当前VC只支持默认UIInterfaceOrientationMaskPortrait方向
4. 方法四:
iOS8~iOS10+目前都有效:
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
return UIInterfaceOrientationMaskLandscapeR
- (BOOL)shouldAutorotate
return YES;
在viewWillAppear && viewWillDisAppear时,强制通过KVC设置Orientation,这里并没有涉及到私有API,不会被拒审,只能说这是一种不安全的方式来实现屏幕旋转。
强制改变当前页面的Orientation:
- (void)landscapeAction:(UIButton *)button
if (self.supportedInterfaceOrientations == UIInterfaceOrientationMaskPortrait) {
[self blackMagicChangeOrientation:UIInterfaceOrientationLandscapeRight];
[self blackMagicChangeOrientation:UIInterfaceOrientationPortrait];
- (void)blackMagicChangeOrientation:(UIInterfaceOrientation)orientation
UIViewController *viewController = [[UIViewController alloc] init];
[viewController setModalPresentationStyle:UIModalPresentationCurrentContext];
viewController.view.frame = CGRectZ
[self.navigationController pushViewController:viewController animated:NO];
NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
[self.navigationController popViewControllerAnimated:NO];
NSLog(@"current self.view. x: %f, y: %f, width: %f, height: %f", self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
看到一坨代码是否想吐,的确,这种方式实际上通过push、pop一个viewController来触发Orientation的改动已影响现有的ViewController,目前测试结果有效。
注意事项:
iOS8及以上版本以下代码失效:
UIViewController *vc = [[UIViewController alloc]init];
[self presentModalViewController:vc animated:NO];
[self dismissModalViewControllerAnimated:NO];
viewWillDisappear、viewWillAppear等VC生命周期内self.view.frame变化,会影响页面布局。
5. 坑爹的交互逻辑
横屏push横屏
采用上述方法四即可实现横屏push横屏效果。
LandscapeViewController *newLandscapeVC = [LandscapeViewController new];
[self.navigationController pushViewController:newLandscapeVC animated:YES];
详情可参考Demo中的LandscapeViewController中pushVC实现。
6. 横屏遇到的一些问题:
0. Plus 横屏冷启动App
为了避免以横屏模式直接启动App,需要在appdidFinishLaunch中先设置好:
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationP
这行代码需要在放在window之前。
可以参考这个
1. iOS10 横屏BUG
估计是系统的bug,在各大视频软件均遇到过这种情况,特别是在未开启锁方向的情况下。
2. 开启App支持方向,且只支持一个角度,需要UIViewController支持方向收敛
为了解决为了解决iOS8及以上首次装机横屏进来的时候出现Alert的时候应用出现错乱情况,收敛支方向,通过UIViewController的Category来实现默认的Orientation。因为iOS8以后系统默认的使用的Altert均为UIViewController,但并没有设置Orientation,结果首次安装用户横屏状态下,页面都错乱了。
3. StatusBarFrame监听通知
TabBarController接受了StatusBarFrame的通知,MainTab上的后面几个ViewController没有展示的情况下,先走到了viewDidLoad,这时View拿到的屏幕Frame都是横屏比例,结果页面就错乱了,最终通过监听StatusBarFrameChange中异常处理解决了提前走到viewDidLoad的情况。
在一个运行数年之久的竖屏App开启横屏模式的确有很大挑战,页面布局,页面跳转原有的逻辑在新模式下都需要改造,也许还有更好的办法解决横屏问题,有待继续研究。
横屏push横屏,非官方推荐交互逻辑,一般采用present形式且无push下个页面的形式,这样设计有点反人类。
参考YouTube、腾讯视频,均采用页面自刷新形式来更新数据源。这样可以减少横屏模式下push方式,减少采坑点。
iOS开发攻城狮
前端搬砖工
努力变全栈
用到的组件1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SDWebImage多个缩略图缓存组件 UICKeyChainStore存放用户账号密码组件 Reachability监测网络状态 DateTools友好...
发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注
09:45字数 61697阅读 3316评论 2喜欢 85 用到的组件 1、通过CocoaPods安装 项目名称 项目信息 AFNetworking 网络请求组件 FM...
用到的组件1、通过CocoaPods安装项目名称项目信息AFNetworking网络请求组件FMDB本地数据库组件SDWebImage多个缩略图缓存组件UICKeyChainStore存放用户账号密码组件Reachability监测网络状态DateTools友好化时间MBP...
此文章转自github:https://github.com/Tim9Liu9/TimLiu-iOS 介绍 这是一个用于iOS开发的各种开源库、开源资料、开源技术等等的索引库. 具体内容 ============================= 版本管理@ 依赖管理@ G...
下边都学会就大神了: 声明:都是网上搜集的,能标明出处的都标了.别只搜集而不看,与君共勉.. 先看完整项目完整App@HackerNews-React-Native用 React Native 完成的 HackerNews 客户端。WeChat实现类似微信朋友圈或者QQ空间...
我自己现在常用的是京东阅读App和百度阅读App,最开始使用的电子书阅读App则是当当读书。
接下来,想谈谈自己对这三款电子书阅读App的使用体会: 1、京东阅读
作为自己目前主要阅读电子书的App,有以下三点吸引我: (1)京东电子书畅读卡
我们经常会遇到这样的情况,那就是对于从来没有做过的事情总是感觉不够自信。每当这个时候,我们总是充满了怀疑、不自信、抵触等情绪。例如戒烟、减肥、写作等等。我们总是怀疑自己的能力,到底能不能做到那件事。 1.那么,什么叫做自信呢? 自信,说白了就是对未曾做过的那件事情的信心。虽...
我想,你也许从未靠近我 我怀抱的玫瑰,它赤诚如火焰 以此遮挡我的忧伤和表情 我眼里只能看见你的世界 不管白天与黑夜,眼里只有你 田野上夭折的稻草和山坡上疯长的罂粟 它们一会儿跟随你的影子 一会儿又在风中狂舞 你不在的时候 空气常常变成吃人的巨浪 吞噬我的骸骨 我只有茫然,而...> 博客详情
摘要: 在处理界面的强制横屏问题时,新手可能会遇到许多问题,这里为大家提供几种方案。
IOS6以后,若想在项目中支持横屏,我们首先需要在plist文件中添加支持横屏的设置,否则有些代码设置将会失效。
有来那个方式设置:
1、在pilist的Supported interface orientations 字段中添加
2、在Xcode的设置中勾选
现在我们来看决定屏幕方向的几个函数:
在IOS6之前,我们只需通过一个函数
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { &&& return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); }
就可以支持指定控制器的旋转。通过新的文档,我们可以看到:
//&Applications&should&use&supportedInterfaceOrientations&and/or&shouldAutorotate..
-&(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
&&&&&NS_DEPRECATED_IOS(2_0,&6_0);
//这个方法在6.0之后被标记为过时的
我们通过下面两个方法来代替:
//是否允许屏幕旋转
-(BOOL)shouldAutorotate{ &&& return YES; } //支持的方向 - (NSUInteger)supportedInterfaceOrientations { &&& return UIInterfaceOrientationMaskLandscapeR } 这是个枚举
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),
通过这两个函数,如果我们需要某个控制器强制方向,我们可以设置支持单一的方向,即可达到目的。
如果你们项目中的RootViewController是导航,你会发现,你在Push出来的视图中添加刚才的代码并没有起作用,原因是导航,并没有进行设置,我们创建一个文件,继承于NavigationController。在里面重写刚才的方法,这么做后,屏幕确实横了过来,并且这个导航push的所有子界面都将横屏,这也不是我们想要的效果。我们想自由的控制每个push出来的界面的屏幕方向,可以在导航里这么做:
-(BOOL)shouldAutorotate{
&&&&return&[self.topViewController&shouldAutorotate];
//支持的方向
-&(NSUInteger)supportedInterfaceOrientations&{
&&&&return&[self.topViewController&supportedInterfaceOrientations];;
我们还需要做一些处理,经过我的测试,导航必须在pop后才会重新调用这些函数,所以我的方法是这样做:弹出一个中间控制器后再POP回来
@implementation&ViewController2
-&(void)viewDidLoad&{
&&&&[super&viewDidLoad];
&&&&//&Do&any&additional&setup&after&loading&the&view.
&&&&[self.navigationController&pushViewController:[[ViewController3&alloc]init]&animated:YES];
@implementation&ViewController3
-&(void)viewDidLoad&{
&&&&[super&viewDidLoad];
&&&&//&Do&any&additional&setup&after&loading&the&view.
&&&&[self.navigationController&popViewControllerAnimated:YES];
这样做,我们就可以自由的控制每个视图控制器的方向了。
同理,如果根视图控制器是tabBar,则我们需要在tabBar中做操作。
如果我们大多是的视图控制器都是一个方向的,只有偶尔的几个会不同,这时候,我们其实可以采取presentationController的方式,然后直接在弹出的控制器中写那两个方法即可。这是最简单的途径了。
专注技术,热爱生活,交流技术,也做朋友。
——珲少 QQ群:
是开源中国针对行业特定技术问题发起的专家问答。
领取条件:受邀参与高手问答的技术专家可以领取
支付宝支付
微信扫码支付
打赏金额: ¥
已支付成功
打赏金额: ¥}

我要回帖

更多关于 ios11横屏bug 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信