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

以下为本人亲测:

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. InnoDB Monitors | SHOW ENGINE INNODB STATUS

    参考 <mysql 5.7手册>,15.17章节 InnoDB监视器提供有关InnoDB内部状态的信息. 这些信息对于性能调优非常有用.其实所谓的监视器,就是 show engine in ...

  2. [Xcode 实际操作]七、文件与数据-(9)编码创建Plist文件(属性列表文件)

    目录:[Swift]Xcode实际操作 本文将演示如何通过编码的方式,创建属性列表文件. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit ...

  3. angular实现表格的全选、单选、部分删除以及全部删除

    昨天自己写了一小段js,在全选的时候有点儿小坑,然后,整理了一下.今天把它贴出来,希望以后还记得. 大家也可以去github上查看或下载:https://github.com/dreamITGirl/ ...

  4. 剑指Offer的学习笔记(C#篇)-- 从上往下打印二叉树

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 一 . 题目解析 了解过二叉树就应该知道,二叉树存在三种遍历方法:前序遍历(根→左→右).中序遍历(左→根→右).后续遍历(左→右→根 ...

  5. elasticsearc 参考资料

    _source 和store http://stackoverflow.com/questions/18833899/in-elasticsearch-what-happens-if-i-set-st ...

  6. 学习笔记之a,b=b,a+b与a=b,b=a+b的区别

       兔子序列中用到的常用的计算方法:a,b=b,a+b 当我们真正去运行的时候,会发现它与a=b,b=a+b是有区别的 实例代码如下: def YY(one): a,b,n=0,1,0 while( ...

  7. [Android]进程间通信的方法

    一.管道 管道是进程间通信中最古老的方式,它包括 无名管道 和 有名管道两种,前者用于父进程和子进程间的通信,后者用于运行于同一台机器上的任意两个进程间的通信. 无名管道由pipe()函数创建. #i ...

  8. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) D

    Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the ...

  9. python isinstance函数 判断元素是否是字符串、int型、float型

    isinstance(1, int) 判断是否是int型isinstance(1.0, float) 判断是否是float型isinstance(s, str) 判断是否是字符串型isinstance ...

  10. python大战机器学习——聚类和EM算法

    注:本文中涉及到的公式一律省略(公式不好敲出来),若想了解公式的具体实现,请参考原著. 1.基本概念 (1)聚类的思想: 将数据集划分为若干个不想交的子集(称为一个簇cluster),每个簇潜在地对应 ...