UIDeviceOrientation 和 UIInterfaceOrientation
有时候,我们处理自动布局时,需要获取到屏幕旋转方向:
以下为本人亲测:
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的更多相关文章
- 关于UIInterfaceOrientation的一个bug
在ios中获取设备当前方向的枚举有UIInterfaceOrientation和UIDeviceOrientation ,前者包含枚举 Unknown//未知 Portrait//屏幕竖直,home键 ...
- 用GPUImage开启相机并且开启滤镜效果
GPUImage提供了GPUImageVideoCamera这么一个类,它的对象能够调用摄像头,并且加上滤镜的效果. //init VideoCamera //这里的两个参数可以设定拍 ...
- 代码处理 iOS 的横竖屏旋转
一.监听屏幕旋转方向 在处理iOS横竖屏时,经常会和UIDeviceOrientation.UIInterfaceOrientation和UIInterfaceOrientationMask这三个枚举 ...
- UIDeviceOrientation UIInterfaceOrientation 区别
UIDeviceOrientation 是机器硬件的当前旋转方向 这个你只能取值 不能设置 UIInterfaceOrientation 是你程序界面的当前旋转方向 这个可以设置 ...
- 需要获取设备方向变化(UIDeviceOrientation)的消息
如果需要获得UIDeviceOrientation的转换消息的话,只需要: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNoti ...
- UIInterfaceOrientation over iOS6 (应用旋转屏幕)
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrien ...
- 谈谈iOS中的屏幕方向
众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...
- Xcode 升级后,常常遇到的遇到的警告、错误,解决方法(转)
从sdk3.2.5升级到sdk 7.1中间废弃了很多的方法,还有一些逻辑关系更加严谨了.1,警告:“xoxoxoxo” is deprecated解决办法:查看xoxoxoxo的这个方法的文档,替换 ...
- IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
转自 http://blog.csdn.net/zzfsuiye/article/details/8251060 概述: 在iOS6之前的版本中,通常使用 shouldAutorotateToInte ...
随机推荐
- c#之quartz任务调度的使用
这里讲下,quartz这种任务调度程序的简单使用 这是使用的quartz的3.x 版本 2.x 版本与此稍有区别,可以在网上查看2.x版本教程 使用语言为c# quartz的使用分为几个步骤 创建一个 ...
- python bbs项目代码分析
def index(request, *args, **kwargs): condition={} type_id = int(kwargs.get("type_id")) if ...
- SWT的基本组件使用
1按钮组件(Button) (1)Button组件常用样式 SWT.PUSH按钮 SWT.CHECK多选按钮 SWT.RADIO单选按钮 SWT.ARROW箭头按钮 SWT.NONE默认按钮 SWT. ...
- echart表格,动态的通过ajax从后台获取数据动态的展示数据
官网上都是介绍的echar表格的展示方法,但是都是静态数据,一开始的时候我总是纳闷,这些数据都是怎么上上去的 , 后来通过一些方法,当然这些方法也不是我自己写出来的,也是通过在网上收集了一下 ,现在才 ...
- Unity 关于时间
一.引言 本篇博客 包括:unity中帧的耗时,时间缩放比例,常用日期时间的获取和计算,测试一段程序的耗时. 二.帧时间 名称 描述 Time.time (只读)表示从游戏开发到现在的时间,会随着游戏 ...
- 1081 Rational Sum(20 分)
Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...
- shell 经典
使用新写法 这里的新写法不是指有多厉害,而是指我们可能更希望使用较新引入的一些语法,更多是偏向代码风格的,比如 尽量使用func(){}来定义函数,而不是func{} 尽量使用[[]]来代替[] 尽量 ...
- Win10 插入耳机后没有声音,拔出后电脑有声音
- ADC5513
一 C5513 u32 ADC5513_GetValue(void){ u32 ADValue,i; bool data_bit = false; C5513_SCK=0; C5513_CS ...
- 整数的lqp拆分
题目大意 lqp在为出题而烦恼,他完全没有头绪,好烦啊… 他首先想到了整数拆分.整数拆分是个很有趣的问题.给你一个正整数N,对于N的一个整数拆分就是满足任意m>0,a1 ,a2 ,a3…am&g ...