有时候,我们处理自动布局时,需要获取到屏幕旋转方向:

以下为本人亲测:

UIInterfaceOrientation:

我们需要在- (void)viewDidLoad或其他方法中添加观察者,检测屏幕的旋转:

    // 监听状态栏旋转方向,即屏幕旋转
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

然后实现方法:

// 监听的是StatusBar
- (void)willChangeStatusBarOrientationNotification:(NSNotification *)notification {
// 此处我们只介绍常用的三种横竖屏
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait) { // 竖屏
NSLog(@"竖屏");
}
if (orientation == UIInterfaceOrientationLandscapeRight) {// 横屏(Home键在右边)
NSLog(@"横屏(Home键在右边");
} if (orientation == UIInterfaceOrientationLandscapeLeft) { // 横屏(Home键在左边)
NSLog(@"横屏(Home键在左边)");
}
}

此处需要说明一下:屏幕的旋转,枚举值里面的 right/left 是以Home方法为准的!和设备旋转是相反的!

UIDeviceOrientation:

同样的,我们需要注册设备旋转的观察者:

    // 设置旋转
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];

实现通知方法:

- (void)didChangeNotification:(NSNotification *)noti {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
switch (deviceOrientation) {
case UIDeviceOrientationPortrait:
NSLog(@"竖屏");
break; case UIDeviceOrientationLandscapeLeft:
NSLog(@"横屏(Home在右边,设备向左边倒)");
break; case UIDeviceOrientationLandscapeRight:
NSLog(@"横屏(Home在左边,设备向右边倒)");
break; case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"横屏(Home在右边,设备向左边倒)");
break; case UIDeviceOrientationFaceUp:
NSLog(@"屏幕向上");
break; case UIDeviceOrientationFaceDown:
NSLog(@"屏幕向下");
break; default:
break;
}
}

注:设备旋转时,枚举值里面的方法,指的是设备倒向的方法。

小技巧:

1.获取屏幕当前状态栏旋转方法

- (UIInterfaceOrientation)appInterface {
return [UIApplication sharedApplication].statusBarOrientation;
}

2.强制屏幕做出旋转

// 2对应相应的枚举值
[[UIDevice currentDevice] setValue:@"" forKey:@"orientation"];

尊重作者劳动成果,转载请注明: 【kingdev】

UIDeviceOrientation 和 UIInterfaceOrientation的更多相关文章

  1. 关于UIInterfaceOrientation的一个bug

    在ios中获取设备当前方向的枚举有UIInterfaceOrientation和UIDeviceOrientation ,前者包含枚举 Unknown//未知 Portrait//屏幕竖直,home键 ...

  2. 用GPUImage开启相机并且开启滤镜效果

    GPUImage提供了GPUImageVideoCamera这么一个类,它的对象能够调用摄像头,并且加上滤镜的效果.     //init VideoCamera     //这里的两个参数可以设定拍 ...

  3. 代码处理 iOS 的横竖屏旋转

    一.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation.UIInterfaceOrientation和UIInterfaceOrientationMask这三个枚举 ...

  4. UIDeviceOrientation UIInterfaceOrientation 区别

    UIDeviceOrientation      是机器硬件的当前旋转方向   这个你只能取值 不能设置 UIInterfaceOrientation   是你程序界面的当前旋转方向   这个可以设置 ...

  5. 需要获取设备方向变化(UIDeviceOrientation)的消息

    如果需要获得UIDeviceOrientation的转换消息的话,只需要: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNoti ...

  6. UIInterfaceOrientation over iOS6 (应用旋转屏幕)

      typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrien ...

  7. 谈谈iOS中的屏幕方向

    众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...

  8. Xcode 升级后,常常遇到的遇到的警告、错误,解决方法(转)

    从sdk3.2.5升级到sdk 7.1中间废弃了很多的方法,还有一些逻辑关系更加严谨了.1,警告:“xoxoxoxo”  is deprecated解决办法:查看xoxoxoxo的这个方法的文档,替换 ...

  9. IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)

    转自 http://blog.csdn.net/zzfsuiye/article/details/8251060 概述: 在iOS6之前的版本中,通常使用 shouldAutorotateToInte ...

随机推荐

  1. django 数据库建表流程,与表结构

    目录 配置数据库 创建表结构 多表关连的设置 自创建关联表方法 自建表 和 ManyToManyField 联合使用 配置数据库 在Django项目的settings.py文件中,配置数据库连接信息: ...

  2. JQuery Easyui/TopJUI 基本树形表格的创建

    <table data-toggle="topjui-treegrid" data-options="id:'menuTg', idField:'id', tree ...

  3. Jmeter4.0----设置集合点_并发(11)

    1.说明 LR中集合点可以设置多个虚拟用户等待到一个点,同时触发一个事务,以达到模拟真实环境下多个用户同时操作,实现性能测试的最终目的. jmeter中使用Synchronizing Timer实现L ...

  4. Jexus 5.8.2

    Jexus 5.8.2 正式发布为Asp.Net Core进入生产环境提供平台支持   Jexus 是一款运行于 Linux 平台,以支持  ASP.NET.PHP 为特色的集高安全性和高性能为一体的 ...

  5. 关于Identityserver4和IdentityServer3 授权不兼容的问题

    使用IdentityServer3 作为授权服务器,如果没有设置证书,而且client又没有设置AccessTokenType = AccessTokenType.Reference,则获取token ...

  6. swift3.0 项目引导页

    项目引导页并不难,使用 UICollectionView就可以完成, 1.首先获取应用程序的版本号,并存入本地,每次有新版本号,和存入本地的版本号,相比较 fileprivate func setup ...

  7. 字符串和byte数组的相互转化

    关于byte[]数组转十六进制字符串: public static String getHexString(byte[] b) throws Exception { String result = & ...

  8. 几种常用排序算法代码实现和基本优化(持续更新ing..)

    插入排序(InsertSort): 插入排序的基本思想:元素逐个遍历,在每次遍历的循环中,都要跟之前的元素做比较并“交换”元素,直到放在“合适的位置上”. 插入排序的特点:时间复杂度是随着待排数组的有 ...

  9. 小技巧:在向导式页面设计中使用hidden型输入可以避免session的使用

    在向导式页面设计中使用hidden型输入可以避免session的使用,从而减小内存开支. 在表单中使用隐藏输入类型<input type="hidden" name=&quo ...

  10. Azure 镜像市场支持一键部署到云

    本视频教程介绍了Azure 镜像市场和一键部署到云. Azure 镜像市场(AMP)由世纪互联运营,是一个联机应用程序和服务市场,它通过独立软件服务商(ISV)能够成为 Azure 客户(Custom ...