方法一:通知中心监听

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. async/await与promise(nodejs中的异步操作问题)

    此文只是粗略介绍使用方法,欲了解核心概念请参考官方文档或其他资料. 举例写文章详情页面的时候的一个场景:首先更改文章详情中的 PV,然后读取文章详情,然后根据文章详情中文章 Id 查阅该文章评论和该文 ...

  2. php+redis 简易的实现文章发布系统(用户投票系统)

    /** * @data 文章发布 * 文章详情散列表中递增ID,讲文章发布者ID写入投票用户集合中,设置投票时间为一周 * 讲文章内容写入文章散列中,讲文章写入文章评分有序集合和文章发布有序集合中 * ...

  3. lxd容器之GPU发现和加载

    lxd gpu设备发现: // /dev/nvidia[0-9]+ type nvidiaGpuCards struct { path string major int minor int id st ...

  4. MES设备支持快速完工

    1) 在菜单界面点击指定快速键 2) 初始界面 3) 一般流程 a) 扫描任务单号,即可完成工序加工 a1) 获取任务单工序的条件 按任务单卡号或配模的模具卡号搜索行状态为O的工序 a2) 工序完工操 ...

  5. Unbutu14.04 启用 root 并禁用guest

    系统环境: Ubuntu14.04 x64 问题描述: Ubuntu14.04的64位系统默认禁用 root 账号登录 解决方式:            1. 为root设置密码 sudo passw ...

  6. 3892: [Usaco2014 Dec]Marathon

    3892: [Usaco2014 Dec]Marathon Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 100[Submi ...

  7. mysql命令[转]

    来自:http://www.cnblogs.com/zhangzhu/archive/2013/07/04/3172486.html 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录my ...

  8. 用递归的方式处理数组 && 把递归方法方法定义到数组的原型上 (这是一次脑洞大开的神奇尝试)

    在 javascript 里,如果我们想用一个函数处理数组 (Array) 中的每个元素,那我们有很多种选择,最简单的当然就是用自带的 forEach 函数(低版本也可以使用 lodash 中的 fo ...

  9. 更加 "深入" 理解多态

    1.1 public abstract class Birds{ //什么样的方法是抽象方法 public abstract void Fly(); } public class YZ:Birds{ ...

  10. 用ListView实现对数据库的内容显示

    用ListView实现对数据库的内容显示 创建一个触发机制 ---------(作用)将数据读入ArrayList集合中 MyBase base = new MyBase(); SQLiteDatab ...