1 首先在Xcode工程配置中设置 项目支持哪些方向上的旋转

从上往下分别是  上、下、左右  如图所示 表示当前项目 支持竖屏、左右横屏。Xcode中的配置是全局的,意味着项目中所有的控制器都默认支持这些方向的旋转。

2 控制器单独控制

Xcode工程配置中设置了全局支持的旋转方向,不过实际项目中经常不是所有的控制器都支持一样的旋转方向,所以需要通过代码实现控制器的单独配置。

为了能看懂代码 这里先介绍下跟旋转有关的三个枚举

UIDeviceOrientation: 设备旋转方向


typedef NS_ENUM(NSInteger, UIDeviceOrientation) {UIDeviceOrientationUnknown,UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottomUIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the rightUIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the leftUIDeviceOrientationFaceUp,              // Device oriented flat, face upUIDeviceOrientationFaceDown             // Device oriented flat, face down
} API_UNAVAILABLE(tvos);

UIInterfaceOrientation:屏幕旋转方向

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} API_UNAVAILABLE(tvos);

UIInterfaceOrientationMask:看代码的定义 可以理解为 用于对UIInterfaceOrientation的描述


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),
} API_UNAVAILABLE(tvos);

然后通过查看官方API可以看到UIViewController中声明了以下三个属性

// 是否支持屏幕旋转  当这个值为NO时  当前控制器只能竖屏@property(nonatomic, readonly) BOOL shouldAutorotate ;// 当前控制器支持的旋转方向@property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations;// 页面出现时的默认旋转方向@property(nonatomic, readonly) UIInterfaceOrientation preferredInterfaceOrientationForPresentation;

这三个属性是只读的,所以没法用set方法去设置,不过可以通过重写get方法的形势来返回想要的值  比如以下代码所示


// 返回yes表示支持旋转
- (BOOL)shouldAutorotate
{return YES;
}// 支持竖屏和横屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape);
}// 默认旋转左边
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{return UIInterfaceOrientationLandscapeLeft;
}

不过要注意的是  如果当前控制器是属于一个导航控制器的 那么导航控制器中要实现

// 是否支持自动转屏
- (BOOL)shouldAutorotate {return self.topViewController.shouldAutorotate;
}// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return self.topViewController.supportedInterfaceOrientations;
}// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return self.topViewController.preferredInterfaceOrientationForPresentation;
}

同样的 如果控制器外面还有一层TabBarViewControllre那么 TabBarViewControllre要实现

// 是否支持自动转屏
- (BOOL)shouldAutorotate {return self.selectedViewController.shouldAutorotate;
}// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {return self.selectedViewController.supportedInterfaceOrientations;
}// 默认的屏幕方向(当前ViewController必须是通过模态出来的UIViewController(模态带导航的无效)方式展现出来的,才会调用这个方法)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}

到了这里 我们就知道了 怎么全局或单独配置某个ViewController是否支持、支持哪些方向的旋转。当手机设备旋转时,界面也会相应的旋转。

3 监听屏幕的旋转

既然某个VC支持了屏幕旋转,也就意味着 该界面的竖屏和横屏是两套不同的UI,所以当屏幕旋转时,监听系统发出的通知,及时刷新UI 做好横竖屏的适配。

// 监听屏幕旋转[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];- (void)orientationDidChange
{switch ([UIDevice currentDevice].orientation) {case UIDeviceOrientationPortrait:self.isFullScreen = NO;break;case UIDeviceOrientationLandscapeLeft:self.isFullScreen = YES;break;case UIDeviceOrientationLandscapeRight:self.isFullScreen = YES;break;default:break;}
}

4 主动控制屏幕的旋转

当锁定了iPhone的方向之后,旋转手机,UIDevice的orientation属性不会改变,同时系统也不会发出UIDeviceOrientationDidChangeNotification通知,此时我们只能通过代码的方式强制修改当前Device的orientation,当orientation改变时,系统会发出UIDeviceOrientationDidChangeNotification通知


//  因为orientation属性是只读的  所有这里通过KVC的形式来设置值  [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:orientation]forKey:@"orientation"];

iOS_控制屏幕旋转相关推荐

  1. 在Android的 设置-显示 中增加控制屏幕旋转方向的选项

    在Android的 设置->显示 中增加控制屏幕旋转方向的选项 参考博文 实现目标 效果局限 代码实现 配置资源文件 界面搭建 功能实现 默认值修改 其他情况 结语 参考博文 Android-x ...

  2. iOS6的控制屏幕旋转技巧

    在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如: ...

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

    在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 来单独控制某个UIViewController的方向,需要哪个viewControlle ...

  4. Android中控制屏幕旋转的相关设置

    转自:http://blog.csdn.net/u012364372/article/details/51088831 一.设定屏幕方向 当指定了屏幕的方向后,屏幕就不会自动的旋转了 有2种方式控制屏 ...

  5. iOS6下关于屏幕旋转的控制

    之前做了一个应用,但由于整应用界面个都是竖屏,不允许横屏,所以一直没有关注这个,昨天开发一个图片预览的类库(类似系统的查看图片),其中一个特性当然需要支持横屏,所以就压找了一下资料,之前已经听闻在屏幕 ...

  6. iOS屏幕旋转 浅析

    一.两种orientation 了解屏幕旋转首先需要区分两种orientation 1.device orientation 设备的物理方向,由类型UIDeviceOrientation表示,当前设备 ...

  7. iOS屏幕旋转及其基本适配方法

    屏幕旋转示例.jpeg 前段时间抽空总结了一下iOS视频播放的基本用法,发现这其中还有一个我们无法绕过的问题,那就是播放界面的旋转与适配.的确,视频播放与游戏类型的App经常会遇到这个的问题.由于至今 ...

  8. IOS屏幕旋转的检测 与 强行切换

    mark – 屏幕的手动切换 [[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLand ...

  9. android 強制屏幕方向,今日精品安卓App推荐:锁定屏幕旋转方向

    [PConline 资讯]不知道各位用安卓的朋友有没有遇到过这样一种情况,玩某个游戏横屏时屏幕向右转,另一个游戏就向左转了,用某些播放器如新闻客户端之类的看视频,甚至还不能横屏.安卓上横屏方向不统一的 ...

最新文章

  1. rapidjson读取json文件_SPARK入门 - json文件读取
  2. 流量回放开源代码Java_流量回放框架 jvm-sandbox-repeater 的实践
  3. 单片机小白学步系列(十四) 点亮第一个LED的程序分析
  4. java中子线程与主线程通信_Android笔记(三十二) Android中线程之间的通信(四)主线程给子线程发送消息...
  5. 演练 青春不常在 0915
  6. OpenBSD4.6 FAQ[一]
  7. 《ASCE1885的源码分析》の简单的进程封装类
  8. 蜗居6个月,苹果漏洞神猎手亮绝招:展示零点击 iOS exploit
  9. ubuntu14.04 设置静态ip
  10. mysql mycat docker_docker-mycat-mysql
  11. LeetCode 63.不同路径II(动态规划)
  12. 网页跳转QQ聊天界面
  13. echarts离线地图
  14. C语言学习——极限值
  15. 【电子电路计算公式】 导线流过电流计算工具,我已经做成一个小工具了(源代码)
  16. android 摄像头 测距,GitHub - infonous/Everest: Android 手机拍照测距
  17. Swift实战-豆瓣电台(四)歌曲列表的展现
  18. 深度强化学习下移动机器人导航避障
  19. 黑马股票底部特征和2018年黑马股票池
  20. finecmsV5.0.8 \finecms\dayrui\controllers\Api.php getshell

热门文章

  1. Python有趣|微博网红大比拼
  2. MongoDB 查询篇 及 数组修改器更新数据
  3. vue 动态设置背景图片,和背景渐变
  4. 你的APP,性能优化了吗?
  5. 配置中心—nacos配置中心
  6. oracle 闪回表定义,oracle闪回表详解
  7. 机器学习5-线性回归算法的代码实现
  8. dcloud IOS 网络不通问题
  9. 头条java 后台一面凉经
  10. 记录 - 收货地址的坑