方法一:通知中心监听

name:

// UIDeviceOrientationDidChangeNotification     允许方向改变的情况下,监听设备方向,与电池条无关

// UIApplicationDidChangeStatusBarOrientationNotification      允许方向改变的情况下,监听电池条方向

-(void)notifitionSatatus{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
  
}
-(void)statusBarOrientationChange:(NSNotification*)notification{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight) // home键靠右
{ }
if (orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左
{ }
if (orientation == UIInterfaceOrientationPortrait)//home在下
{ }
if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{ }
NSLog(@"%ld",orientation);
}

注意:在没有打开屏幕旋转的情况下,该通知无效

另外:viewcontrller内部控制及代理

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
} - (BOOL)shouldAutorotate
{
return YES;
} - (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
//
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"UIViewController will rotate to Orientation: %d", toInterfaceOrientation);
} - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
NSLog(@"did rotated to new Orientation, view Information %@", self.view);
}

appdelegate中有相应改变其旋转方向的方法

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}

方法二:陀螺仪监测

- (void)startMotionManager{
if (_motionManager == nil) {
_motionManager = [[CMMotionManager alloc] init];
}
  //检测时间间隔
_motionManager.deviceMotionUpdateInterval = 1.0;
if (_motionManager.deviceMotionAvailable) {
NSLog(@"Device Motion Available");
[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES]; }];
} else {
NSLog(@"No device motion on device.");
[self setMotionManager:nil];
}
}
- (void)handleDeviceMotion:(CMDeviceMotion *)deviceMotion{
double x = deviceMotion.gravity.x;
double y = deviceMotion.gravity.y;
if (fabs(y) >= fabs(x))
{
if (y >=){
//UIDeviceOrientationPortraitUpsideDown;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortraitUpsideDown];
}
else{
// UIDeviceOrientationPortrait;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationPortrait];
}
}
else
{
if (x >= ){
// UIDeviceOrientationLandscapeRight;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeRight];
}
else{
// UIDeviceOrientationLandscapeLeft;
[self.delegate actionWithDeviceOrientation:UIDeviceOrientationLandscapeLeft];
}
}
}
//停止检测
-(void)endMotionManager{
[_motionManager stopDeviceMotionUpdates];
}

ios 检测屏幕方向的更多相关文章

  1. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  2. 监听iOS检测屏幕旋转状态,不需开启屏幕旋转

    -(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...

  3. ios 设置屏幕方向的两种方法

    第一种:通过人为的办法改变view.transform的属性. 具体办法: view.transform一般是View的旋转,拉伸移动等属性,类似view.layer.transform,区别在于Vi ...

  4. iOS 检测屏幕是否锁定 🔓 / 🔒

    1. 导入头文件 #import <notify.h> 2. 给 CFNotificationCenter 添加观察者 - (void)addLockStatusObserver { CF ...

  5. Android开发之屏幕方向

    一.处理屏幕方向变化的两种技术 1.锚定方法 2.调整大小和重新定位,这种方法一般是分别为横向和纵向两种模式各自定义用户界面xml界面文件,当方向变化时读取对应的界面配置文件即可. 二.检测屏幕方向改 ...

  6. Windows phone开发 页面布局之屏幕方向

    (博客部分内容参考Windows phone开发文档) Windows phone的屏幕方向是利用Windows phone设备的方向传感器提供的数据实现切换的. Windows Phone支持纵向和 ...

  7. 谈谈iOS中的屏幕方向

    众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...

  8. iOS如何用代码控制以不同屏幕方向打开新页面?

    转载:http://blogread.cn/it/article/7765?f=wb#original 代码示例:https://github.com/johnlui/Swift-On-iOS/tre ...

  9. iOS 屏幕方向

    参考文章:http://www.tuicool.com/articles/e2q6zi 一般的应用,只会支持竖屏正方向一个方向,支持多个屏幕方向的应用还是比较少的. 当时也没搞明白,所以直接就设置了正 ...

随机推荐

  1. 程序点滴001_Python模拟点阵数字

    尝试过很多编程语言,写过不少程序(当然,基本上都是些自娱自乐或给自己用的工具类的小玩意儿),逐渐认识到编写程序是一个不断完善.不断优化的过程——编程首先要有一个想法(目标),围绕这个目标形成最基本的功 ...

  2. redis 扩展下载

    自己保存一下  免得以后忘记还得重新找T-T http://windows.php.net/downloads/pecl/snaps/redis/2.2.5/ phpinfo查看自己的php信息下载对 ...

  3. 阿里云开发之OSS数据迁移

    最近由于项目需求,需要将一个aliyun账号下的oss数据导入到两一个aliyun账号下的oss,经过一番坎坷,最终搞定. 1.查看oss数据迁移官方文档,我是在本地windows电脑上进行操作的,先 ...

  4. 徒手用Java来写个Web服务器和框架吧<第三章:Service的实现和注册>

    徒手用Java来写个Web服务器和框架吧<第一章:NIO篇> 徒手用Java来写个Web服务器和框架吧<第二章:Request和Response> 这一章先把Web框架的功能说 ...

  5. Think PHP 基础

    ThinkPHP 一.什么框架: 一堆代码的集合,里边有变量.函数.类.常量,里边也有许多设计模式MVC.AR数据库.单例等等. 框架可以节省我们50-60%的工作量,我们全部精力都集中在业务层次. ...

  6. sqlserver的一些小知识点

    1.高效分页sql和储存过程 select top 每页条数 * from ( select ROW_NUMBER() over (order by id)as nid ,* from table01 ...

  7. BOM基础(一)

    学完了js的基础语法和DOM之后,就要要看看javascript中最后一项BOM了.BOM,全称brower document model,翻译过来就是浏览器对象模型.DOM是文档对象模型,属于BOM ...

  8. react学习总结

    http://www.runoob.com/react/react-tutorial.html一般先看一些中文的简单的介绍和一些基本概念http://reactjs.cn/react/docs/get ...

  9. 变量 || 基本数据类型 || if、while语句

    变量名:只能由数字.字母.下划线组成且不能以数字开头:变量名不可以是python内部的关键字   基本数据类型:数字.字符串.布尔值(True/False)   [if条件语句] if 条件:     ...

  10. CAAnimation动画--(旋转/缩放/移动/闪烁)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p2 { margin: 0.0px 0. ...