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

以下为本人亲测:

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. [转] ios数组基本用法和排序

    http://blog.csdn.net/daiyelang/article/details/18726947 1.创建数组 // 创建一个空的数组 NSArray *array = [NSArray ...

  2. Unity 播放的声音比声音文件小很多-AudioListener-AudioClip

    今天做愤怒的小鸟时,播放的时候非常非常小,怎么也查不到原因,就去问群里的大佬.原来, 播放音乐的方法: AudioSource.PlayClipAtPoint(audioclip, transform ...

  3. SVN Trunk Tag Branch

    http://blog.csdn.net/vbirdbest/article/details/51122637

  4. [WebShow系列] 倒计时展示相关问题

    WebShow内置了倒计时功能. 后台参数维护: 倒计时参数说明: 倒计时基准时间设置(秒数):假设设置为90,也就是从1分30秒开始倒计时,同时有开始提示音. 倒计时提示1剩余秒数:假设设置为30, ...

  5. JS 检测字符串是否还有某个字符

    function filer(s) { var str = "字符串"; if (str.indexOf(s) == -1) { alert("没有"); } ...

  6. P1829 [国家集训队]Crash的数字表格 / JZPTAB 莫比乌斯反演

    又一道...分数和取模次数成正比$qwq$ 求:$\sum_{i=1}^N\sum_{j=1}^Mlcm(i,j)$ 原式 $=\sum_{i=1}^N\sum_{j=1}^M\frac{i*j}{g ...

  7. CodeForces - 608A-Saitama Destroys Hotel(模拟)

    Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to o ...

  8. js和jq中常见的各种位置距离之offsetLeft/clientLeft/scrollLeft (一)

    offsetLeft offsetTop offsetWidth offsetHeight offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(不包括元 ...

  9. HDU 1114 Piggy-Bank 完全背包 dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包的题目,要求输出最小价值.然后一定要把给出的背包重量全部用完. 就是问一个背包为k的大小,n件物品 ...

  10. (转)COBBLER无人值守安装

    COBBLER无人值守安装 说在最前面的话 在看Cobbler之前请大家先看一下Kickstart无人值守安装,了解一下Cobbler的实现原理.但是Cobbler是独立的,不需要先安装Kicksta ...