方法一:通知中心监听

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. javascript 常用api

    常用API合集 来源于:https://www.kancloud.cn/dennis/tgjavascript/241852 一.节点 1.1 节点属性 Node.nodeName //返回节点名称, ...

  2. 深入JSP学习

    常规JSP JSP页面最终会由容器来生成Servlet类的,比如Tomcat容器生成JSP的Servlet类放在work目录里.因此在JSP里可以用很多简化的语法供容器使用,这篇就来整理一下. JSP ...

  3. js原生Ajax 的封装和原理

    原理及概念 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是一种用于创建快速动态网页的技术. 动态网页:是指可以通过服务器语言结合数 ...

  4. KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-yilianyun

    koahub-yilianyun 微信易联云打印机接口 koahub-yilianyun易联云打印机node接口 Installation $ npm install koahub-yilianyun ...

  5. server error in '/' applecation----Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\xx' -- 'Access is denied

    今天在阿里云虚拟机上部署新站点后出现下面的错误:server error in '/' applecation Compiler Error Message: CS0016: Could not wr ...

  6. Codevs3278[NOIP2013]货车运输

    3287 货车运输 2013年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond      题目描述 Description A 国有 ...

  7. 1297: [SCOI2009]迷路

    1297: [SCOI2009]迷路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 652  Solved: 442[Submit][Status] ...

  8. Android:百度地图 + 百度导航

    地图SDK 开发指南:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/introduction 导航SDK开发指南:http://lb ...

  9. maven 的 snapshot 和 release 的区别

    release 是发行版,通常正式发布使用. snapshot 快照版本.在开发过程中,每提交一次修改,编译好响应的jar包,都会更新到仓库中,所有添加了这个jar包的依赖都会把远程仓库里的jar包更 ...

  10. KVC与KVO理解

    转载:https://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 理解 KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲 ...