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

以下为本人亲测:

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. IT兄弟连 JavaWeb教程 监听器2

    4  监听HttpSession域对象的创建和销毁 HttpSessionListener接口用于监听HttpSession对象的创建和销毁. 创建一个Session时,激发sessionCreate ...

  2. iOS 根据文字字数动态确定Label宽高

    iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...

  3. js原型和构造函数

    前言 从应用层面深入理解原型模式和js中的构造函数. 构造函数(constructor) js中的任何对象都有自己的构造函数.js中使用字面量声明的普通对象({})或数组([])等子对象本质上都是使用 ...

  4. mongodb-win

    http://www.cnblogs.com/clsn/p/8214194.html#auto_id_22 Mongodb的作用: 用于对象及JSON数据的存储,它存的都是对象比如字典, 数据库,无论 ...

  5. STP-11-多生成树:IEEE 802.1s

    IEEE802.1s多生成树(MultipleSpanningTrees,MST)有时也称为多STP(MultipleSTP,MSTP),它定义了在使用802.1QVLAN网络中,部署多实例STP的标 ...

  6. C.One Piece

    链接:https://ac.nowcoder.com/acm/contest/908/C 题意: Luffy once saw a particularly delicious food, but h ...

  7. php:判断 是否开启 SSL,CURL,ZIP,GD2,MYSQL,是否安装MEMCACHED

    对于php的开发环境,通常需要去先判断下一些扩展和服务时不时已经可用~ 看过的欢迎拍砖,给意见~~ <?php /** * 判断 是否开启 SSL,CURL,ZIP,GD2,MYSQL,是否安装 ...

  8. Function.prototype.apply.call

    我们先从一道简单的题目开始,前几天在git上看到的: 定义log方法,它可以代理console.log的方法.log(1,2,3)  =>  1 2 3 通常,你的答案会是这样的: functi ...

  9. 第一课:K线

    1       K线是根据价格走势中形成的四个价位(开盘价.收盘价.最高价.最低价)绘制而成的.K线是最基本的描述股价涨跌的表现符号(记录某种股票一天的价格变动情况). K线构造的四个价格因素:开盘价 ...

  10. JAVA基础之字节流与字符流

    个人理解: IO流就是将数据进行操作的方式,因为编码的不同,所以对文件的操作就产生两种.最好用字节流,为了方便看汉字等,(已经确定文字的话)可以使用字符流.每个流派也就分为输入和输出,这样就可以产生复 ...