谈谈iOS中的屏幕方向
众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientation这两种方式来获取设备的屏幕方向。
其中UIDeviceOrientation包括以下几种枚举值
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top
UIDeviceOrientationLandscapeLeft, // Device oriented horizontally, home button on the right
UIDeviceOrientationLandscapeRight, // Device oriented horizontally, home button on the left
UIDeviceOrientationFaceUp, // Device oriented flat, face up
UIDeviceOrientationFaceDown // Device oriented flat, face down
} __TVOS_PROHIBITED;
其中UIInterfaceOrientation包括以下几种枚举值
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;
可以看出UIInterfaceOrientation本质其实就是UIDeviceOrientation,但是这不是重点,说说我最近遇到的坑吧。
首先外部是一个tableview的列表页,cell里面有视频可以播,需求是列表页不需要重力感应,也就说一直保持垂直方向,但是cell中的视频点开后需要全屏,这个视频是要有重力感应的。
这时会出现一个问题,如果我手机保持横屏,第一次进入视频,视频并不会改变方向,并且UIDeviceOrientation打印出来为UIDeviceOrientationPortrait,因为我在视频的controller里才beginGeneratingDeviceOrientationNotifications,[UIDevice currentDevice].orientation在没有遇到方向改变的时候,他是不会更新状态的!!!所以就造成了与实际方向不符的情况。而父页面,也就是列表页的UIInterfaceOrientation始终垂直,所以[UIApplication sharedApplication].statusBarOrientation也是用不了的,肿么办!!!
于是乎,别人推荐了CMMotionManager来对方向进行判断,CMMotionManager是从属于Core Motion框架,利用iPhone上的重力加速计芯片来捕捉手机的各种加速值。
代码如下:
- (void)initializeMotionManager{
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = .;
motionManager.gyroUpdateInterval = .; [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (!error) {
[self outputAccelertionData:accelerometerData.acceleration];
}
else{
NSLog(@"%@", error);
}
}];
}
- (void)outputAccelertionData:(CMAcceleration)acceleration{
UIInterfaceOrientation orientationNew; if (acceleration.x >= 0.75) {
orientationNew = UIInterfaceOrientationLandscapeLeft;
}
else if (acceleration.x <= -0.75) {
orientationNew = UIInterfaceOrientationLandscapeRight;
}
else if (acceleration.y <= -0.75) {
orientationNew = UIInterfaceOrientationPortrait;
}
else if (acceleration.y >= 0.75) {
orientationNew = UIInterfaceOrientationPortraitUpsideDown;
}
else {
// Consider same as last time
return;
} if (orientationNew == orientationLast)
return; orientationLast = orientationNew;
}
这样就可以实时的获得当前设备的方向啦
谈谈iOS中的屏幕方向的更多相关文章
- (转)如何处理iOS中照片的方向
如何处理iOS中照片的方向 31 May 2015 • 7 min. read • Comments 使用过iPhone或者iPad的朋友在拍照时不知是否遇到过这样的问题,将设备中的照片导出到Wind ...
- 详解Android中的屏幕方向
屏幕方向 是对Activity而言的,所以你可以在AndroidManifest.xml 文件中,通过<activity> 标记的screenOrientation 属性进行设定,例如: ...
- 谈谈 iOS 中图片的解压缩
原文 对于大多数 iOS 应用来说,图片往往是最占用手机内存的资源之一,同时也是不可或缺的组成部分.将一张图片从磁盘中加载出来,并最终显示到屏幕上,中间其实经过了一系列复杂的处理过程,其中就包括了对图 ...
- 【转】谈谈 iOS 中图片的解压缩
转自:http://blog.leichunfeng.com/blog/2017/02/20/talking-about-the-decompression-of-the-image-in-ios/ ...
- 谈谈iOS中的锁
1 前言 近日工作不是太忙,刚好有时间了解一些其他东西,本来打算今天上午去体检,但是看看天气还是明天再去吧,也有很大一个原因:就是周六没有预约上!闲话少说,这里简单对锁来个简单介绍分享. 2 目录 第 ...
- 如何处理iOS中照片的方向
使用过iPhone或者iPad的朋友在拍照时不知是否遇到过这样的问题,将设备中的照片导出到Windows上时,经常发现导出的照片方向会有问题,要么横着,要么颠倒着,需要旋转才适合观看.而如果直接在这些 ...
- 谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: https://github.c ...
- 转:谈谈iOS中粘性动画以及果冻效果的实现
在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲: 第一个分享的主题是 ...
- IOS中获取屏幕尺寸
//app尺寸,去掉状态栏 CGRect appRect = [UIScreen mainScreen].applicationFrame; NSLog(@"%f, %f, %f,%f&qu ...
随机推荐
- 配置hadoop
1.$ tar -zxvf hadoop-1.0.3.tar.gz 2.添加hadoop到环境变量 root登陆: sudo su 修改环境变量:vi /etc/environment 添加: / ...
- sublime text 3 3083 注册码
亲试,可用! —– BEGIN LICENSE —– Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 B ...
- 3D游戏编程大师技巧──2D引擎的编译问题
接上一篇文章,这里将介绍2D引擎的编译,从现在开始才真正进入<3D游戏编程大师技巧>的学习.本书的第一.二章只是简介了游戏编程和windows编程,从第三章开始才是介绍<window ...
- Hibernate之缓存的原理
一.关于缓存: 其实对于缓存而言,它其实就是一块内存空间,在这个空间中存放了相互关联的持久化对象, 也就是存在于Session缓存内的对象,那么Session负责根据持久化对象的状态变化来同步的更新数 ...
- input输入时光标位置靠上问题解决
在css中如果我们定义了input高度在输入时会发现光标位置靠上了不在居中了,在Chrome浏览器中,当设置了line-height时,input无文字,光标高度与line-height一致:inpu ...
- Ubuntu上Grafana 监控 Docker的技巧
导读 Grafana 是一个有着丰富指标的开源控制面板.在可视化大规模测量数据的时候是非常有用的.根据不同的指标数据,它提供了一个强大.优雅的来创建.分享和浏览数据的方式. 它提供了丰富多样.灵活的图 ...
- 理解Angular中的$apply()以及$digest()
$apply()和$digest()在AngularJS中是两个核心概念,但是有时候它们又让人困惑.而为了了解AngularJS的工作方式,首先需要了解$apply()和$digest()是如何工作的 ...
- optparse
Python 有两个内建的模块用于处理命令行参数: 一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数: 另一个是 optparse,它功能强大 ...
- JAVA基础学习——1.0 Java概述
Java语言 SUN公司 1995年推出的高级编程语言 ■ 主要应用方向 Web开发和Android开发 ■ 主要特点 平台无关性:能运行于不同的平台上 安全性:去掉了指针操作,内存由操作 ...
- .NET MVC Filter异常处理
MVC程序中自带的HandleErrorAttribute,来处理异常,不在显示黄页.前提是在web.config 中 system.web中关闭customerError选项. 但是很多情况下调试异 ...