在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewController支持旋转,只需要重写shouldAutorotateToInterfaceOrientation方法。

但是iOS 6里屏幕旋转改变了很多,之前的 shouldAutorotateToInterfaceOrientation 被列为 DEPRECATED 方法,查看UIViewController.h文件也可以看到:

// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);

程序将使用如下2个方法来代替:

- (BOOL)shouldAutorotate;

- (NSUInteger)supportedInterfaceOrientations;

除了重写这个2个方法,IOS6之后要旋转还有一些需要注意的地方,下面会细述。另外还有一个硬性条件,需要在Info.plist文件里面添加程序支持的所有方向,可以通过以下2种方式添加

1.

2.

另外要兼容IOS6之前的系统,要保留原来的 shouldAutorotateToInterfaceOrientation 方法,还有那些 willRotateToInterfaceOrientation 等方法。

自动旋转设置:控制某个viewController旋转并不是像IOS5或者IOS4一样在这个viewController里面重写上面那2个方法,而是需要在这个viewController的rootViewController(根视图控制器)里面重写,怎么解释呢?就是最前面的那个viewController,直接跟self.window接触的那个controller,比如以下代码:

UIViewController *viewCtrl = [[UIViewController alloc] init];

UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];

if ([window respondsToSelector:@selector(setRootViewController:)]) {

self.window.rootViewController = navCtrl;

} else {

[self.window addSubview:navCtrl.view];

}

如果需要设置viewCtrl的旋转,那么不能在UIViewController里面重写shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面设置,又因为UINavigationController是系统控件,所以这里需要新建一个UINavigationController的子navigationController的子类,然后在里面实现shouldAutorotate和supportedInterfaceOrientations方法,比如:

-(NSUInteger)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskAllButUpsideDown;

}

- (BOOL)shouldAutorotate{

return YES;

}

eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2个控制旋转的方法就应该写在UIViewController里面!

eg2:如果viewCtrl又pushViewController到viewCtrl2,需要设置viewCtrl2的旋转,怎么办呢? 还是在navCtrl里面控制,因为viewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的写法都是

UIViewController *viewCtrl2 = [[UIVewController alloc] init];

[self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];

所以要控制一个UINavigationController push到的所有viewController的旋转,那么就得在navCtrl里面区分是哪个viewController,以便对他们一一控制!同样如果rootViewController是UITabbarController,那么需要在子类化的UITabbarController里面重写那2个方法,然后分别控制!

但是有时候我初始化UINavigationController的时候,并不知道所有我所有需要push到的viewController,那么这里有一个通用的方法,就是让viewController自己来控制自己,首先在navCtrl里面的实现方法改为以下方式:

- (BOOL)shouldAutorotate

{

return self.topViewController.shouldAutorotate;

}

- (NSUInteger)supportedInterfaceOrientations

{

return self.topViewController.supportedInterfaceOrientations;

}

全部调用self.topViewController,就是返回当前呈现出来的viewController里面的设置,然后在viewCtrl、viewCtrl2等等这些viewController里面重写shouldAutorotate和supportedInterfaceOrientations,以方便设置每个viewController的旋转

eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋转设置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包装过的viewCtrl3,那么就应在新包装的navController、tabbarController里面设置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面设置

手动旋转手动旋转也有2种方式,一种是直接设置 UIDevice 的 orientation,但是这种方式不推荐,上传appStore有被拒的风险:

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];

}

第二种是假旋转,并没有改变 UIDevice 的 orientation,而是改变某个view的 transform,利用 CGAffineTransformMakeRotation 来达到目的,比如:

self.view.transform = CGAffineTransformMakeRotation(M_PI/2)

下面讲解采用第二种方式的各版本手动旋转:

思想是首先设置 statusBarOrientation,然后再改变某个view的方向跟 statusBarOrientation 一致!

那既然是旋转,最少也得有2个方向,那么还是少不了上面说的那个硬性条件,先在plist里面设置好所有可能需要旋转的方向。既然是手动旋转,那么就要关闭自动旋转:

- (BOOL)shouldAutorotate{

return NO;

}

手动触发某个按钮,调用方法,这个方法的实现如下:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

self.view.transform = CGAffineTransformMakeRotation(M_PI/2);

self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);

注意:

1. 只需要改变self.view.transform,那么self.view的所有subview都会跟着自动变;其次因为方向变了,所以self.view的大小需要重新设置,不要使用self.view.frame,而是用bounds。

2. 如果shouldAutorotate 返回YES的话,下面设置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情况下才管用!

强制旋转屏幕最近接手了一个项目,正常情况下使用查看图片是没问题的。

用到了 MWPhotoBrowser 这个第三方图片浏览库。

不过发现了一个问题,就是设备横屏modal这MWPhotoBrowser的时候,发生了图片位置错乱。

实在没办法,所以想到了一个馊主意。

就是modal的时候使用代码把设备强制旋转回去。

//UIDevice+WJ.h

@interface UIDevice (WJ)

/**

*  强制旋转设备

*  @param  旋转方向

*/

+ (void)setOrientation:(UIInterfaceOrientation)orientation;

@end

//UIDevice+WJ.m

#import "UIDevice+WJ.h"

@implementation UIDevice (WJ)

//调用私有方法实现

+ (void)setOrientation:(UIInterfaceOrientation)orientation {

SEL selector = NSSelectorFromString(@"setOrientation:");

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];

[invocation setSelector:selector];

[invocation setTarget:[self currentDevice]];

int val = orientation;

[invocation setArgument:&val atIndex:2];

[invocation invoke];

}

@end

ios 旋转屏幕试图切换_总结iOS App开发中控制屏幕旋转的几种方式相关推荐

  1. ios 行间距和段落间距_如何在Microsoft Word中控制行间距和段落间距

    ios 行间距和段落间距 There are lots of reasons you might want to change the amount of space between lines in ...

  2. vue样式 引入图片_详解Vue.js中引入图片路径的几种方式

    vue --version 3.6.3 记录总结一下的Vue中引入图片路径的几种书写方式 vue中静态资源的引入机制 Vue.js关于静态资源的官方文档 静态资源可以通过两种方式进行处理: 在 Jav ...

  3. iOS开发中UIImageView逆时针旋转,并得到旋转后的图片

    很多小伙伴会用系统的动画旋转,但都是顺时针的,但是开发中有些场景需要用到逆时针旋转效果更好,比方说tableView的 展开/收起 指示箭头方向的变换,如果是顺时针复位,就会显得特别别扭.以下一段代码 ...

  4. java做直播需要哪些技术_直播APP开发中需要解决哪些技术难点?千联信息

    泛娱乐发展的火热,网红经济不断发酵的今天,不少企业靠着开发直播APP狠狠的赚了一波,导致很多人眼红,也想投入其中. 那么开发一款直播APP开发中需要解决哪些技术难点?小编在这里就来为大家解答: 视频直 ...

  5. 面试中sql调优的几种方式_面试方式

    面试中sql调优的几种方式 The first question I ask someone in an interview for a cybersecurity position is, &quo ...

  6. Android短视频app开发中如何实现上下滑动切换效果

    在大部分短视频app开发中,都会在app内增加上下滑动切换视频的功能,即下滑切换到下一条短视频,上滑切回到上一条.这种机制可以给用户带来良好的视觉体验,云豹作为优秀的app源码供应商,在该效果的实现上 ...

  7. 开发app用户协议_兰州移动APP开发用户体验设计不应该做的事

    接着讲述关于移动APP开发的用户体验设计应该不做什么?以下是兰州东方商易的移动APP开发设计人员在处理APP开发的UX设计时应避免的一系列要点: 不要限制互动大多数移动APP开发设计者都建议您避免使用 ...

  8. 【整理】ABAP开发中的屏幕跳转

    ABAP开发中的屏幕跳转 这里介绍常见的几种在开发中常用到的事务代码跳转功能. 1.最常用到的是"SET PARAMETER"语句赋值,然后再使用"CALL TRANSA ...

  9. python读取图像数据流_浅谈TensorFlow中读取图像数据的三种方式

    本文面对三种常常遇到的情况,总结三种读取数据的方式,分别用于处理单张图片.大量图片,和TFRecorder读取方式.并且还补充了功能相近的tf函数. 1.处理单张图片 我们训练完模型之后,常常要用图片 ...

最新文章

  1. 【2021年度训练联盟热身训练赛第二场】Soccer Standings(python)
  2. 20165230 2017-2018-2 《Java程序设计》第9周学习总结
  3. Benny:只处理那些NVARCHAR字段,并且NTEXT也是小于4000个字符时的情况.不管类型.只管长度....
  4. 深度学习资料汇总(满满的干货)
  5. java基础知识点(6)——循环语句for-while
  6. jquery name选择器_Jquery--1
  7. windows本地凭据备份与还原
  8. 【免费下载】2021年11月热门报告盘点(附热门报告列表及下载链接)
  9. 在超链接href中实现form的提交
  10. shortcut switch in terminal start pos end pos
  11. Fiddler—PC上实现手机的抓包
  12. 时间协议ntp服务器,时间服务器NTP搭建及NTP协议简介
  13. Windows中USB大容量存储设备无法启动
  14. 考研基础高等数学笔记
  15. xctf攻防世界 REVERSE 高手进阶区 re2-cpp-is-awesome
  16. 一款实用的web截图工具(一)
  17. 为什么“高大上”的算法工程师变成了数据民工?
  18. Python的列表操作
  19. SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)
  20. 通过java代码连接数据库

热门文章

  1. TextSnake文本检测
  2. matlab搭配循环的函数按列将生成的数据写入文件
  3. 常见的电子商务模式理解
  4. intllij idea 快捷键 Mac
  5. wuzhicms 查看模板中的所有可用变量和值
  6. Git 版本控制 在 WIN 下的一些使用方法
  7. Server SAN:弄潮儿云计算时代
  8. 绕过SQL注入限制的方法
  9. 【原创】软件团队建设和管理--之我见
  10. [转载] python如何删除对象属性_Python3基础 delattr 删除对象的属性