1. -(void)rotation_icon:(float)n {
  2. UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>][self.view viewWithTagName:@"home_history"];
  3. UIButton *cam_btn = [self.view viewWithTagName:@"cam_btn"];    UIButton *cut_btn = [self.view viewWithTagName:@"cut_btn"];      UIButton *light_btn=[self.view viewWithTagName:@"light_btn"];
  4. history_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
  5. cam_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
  6. cut_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
  7. light_btn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
  8. }
  9. - (void)orientationChanged:(NSNotification *)note  {      UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
  10. switch (o) {
  11. case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom
  12. [self  rotation_icon:0.0];
  13. break;
  14. case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top
  15. [self  rotation_icon:180.0];
  16. break;
  17. case UIDeviceOrientationLandscapeLeft:      // Device oriented horizontally, home button on the right
  18. [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
  19. [self  rotation_icon:90.0*3];
  20. break;
  21. case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left
  22. [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
  23. [self  rotation_icon:90.0];
  24. break;
  25. default:
  26. break;
  27. }
  28. }
  29. -(void)viewWillDisappear:(BOOL)animated {
  30. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  31. UIDevice *device = [UIDevice currentDevice]; //Get the device object
  32. [nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];
  33. }
  34. - (void)viewDidAppear:(BOOL)animated {
  35. // Do any additional setup after loading the view from its nib.
  36. //----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----
  37. UIDevice *device = [UIDevice currentDevice]; //Get the device object
  38. [device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation
  39. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
  40. [nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:device];
 
======================
 
iPad iPhone  屏幕旋转检测的方法
 

在特别的场景下,需要针对屏幕旋转作特殊处理。在ios系统下实现相关的功能还是比较方便的。

我下面介绍两种方法:

1.注册UIApplicationDidChangeStatusBarOrientationNotification通知(举例:在一个viewcontroller类的viewdidload中注册该通知),示例代码如下:

[[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)

{

//

}

if (orientation == UIInterfaceOrientationPortraitUpsideDown)

{

//

}

}

注意这种方式监听的是StatusBar也就是状态栏的方向,所以这个是跟你的布局有关的,你的布局转了,才会接到这个通知,而不是设备旋转的通知。

当我们关注的东西和布局相关而不是纯粹设备旋转,我们使用上面的代码作为实现方案比较适合。

2.注册UIDeviceOrientationDidChangeNotification通知(举例:我们同样在一个viewcontroller类的viewdidload中注册该通知),示例代码如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

- (void)orientChange:(NSNotification *)noti

{

NSDictionary* ntfDict = [noti userInfo];

UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;

/*

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   */

switch (orient)

{

case UIDeviceOrientationPortrait:

break;

case UIDeviceOrientationLandscapeLeft:

break;

case UIDeviceOrientationPortraitUpsideDown:

break;

case UIDeviceOrientationLandscapeRight:

break;

default:

break;

}

}

注意到这种方式里面的方向还包括朝上或者朝下,很容易看出这个完全是根据设备自身的物理方向得来的,当我们关注的只是物理朝向时,我们通常需要注册该通知来解决问题(另外还有一个加速计的api,可以实现类似的功能,该api较底层,在上面两个方法能够解决问题的情况下建议不要用,使用不当性能损耗非常大)。

 

监听iOS检测屏幕旋转状态,不需开启屏幕旋转-b的更多相关文章

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

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

  2. 短信状态监听 - iOS

    当使用 App 时若短信介入需要对当前状态进行监听操作,根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后续功能铺路. #import <Messa ...

  3. 截屏状态监听 - iOS

    既接到电话状态监听的需求之后再次添加了截屏状态的监听,当使用 App 时若用户执行截屏操作需要对当前状态进行监听操作,下面有两种方法,其中可以替换截屏的图片内容(Plan A),也可以弹出提示框(Pl ...

  4. iOS 实时监听app的网络连接状态

    实际iOS开发中,在网络通信中我们大部分使用第三方(只谈短链),譬如 AFNetworking.ASIHttpRequest(这个停更了,想必现在没多少人用),swift的 Alamofire 等. ...

  5. 电话状态监听 - iOS

    今天接到一个监听状态的需求,当使用 App 时若电话介入需要对当前状态进行监听操作(注:并非通话内容),根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后 ...

  6. 监听列表ListVIew的滑动状态

    /*监听列表的滑动状态:暂时用不到 * SCROLL_STATE_FLING 时让图片不显示,提高滚动性能让滚动小姑更平滑 * SCROLL_STATE_IDLE 时显示当前屏幕可见的图片*/ mLi ...

  7. Android监听外部存储设备的状态(SD卡、U盘等等)

    近期在项目中须要对外部存储设备的状态进行监听,所以整理了此笔记,以便日后查看. 外部存储设备的状态变化时发出的广播 对照不同状态下的广播 1. 插入外部SD卡时: 2. 移除外部SD卡时: 3. 连接 ...

  8. Android开发——监听Android手机的网络状态

    0. 前言 在Android开发中监听手机的网络状态是一个常见的功能,比如在没网的状态下进行提醒并引导用户打开网络设置,或者在非wifi状态下开启无图模式等等.因此本篇将网上的资料进行了整理总结,方便 ...

  9. 监听事件动态改变dom状态

    html代码: <table class="table table-striped"> <thead> <tr> <th>分类ID& ...

随机推荐

  1. Apache的prefork模式和worker模式(转)

    prefork模式这个多路处理模块(MPM)实现了一个非线程型的.预派生的web服务器,它的工作方式类似于Apache 1.3.它适合于没有线程安全库,需要避免线程兼容性问题的系统.它是要求将每个请求 ...

  2. careercup-树与图 4.7

    4.7 设计并实现一个算法,找出二叉树中某两个结点的第一个共同祖先.不得将额外的结点储存在另外的数据结构中.注意:这不一定是二叉查找树. 解答 本题的关键应当是在Avoid storing addit ...

  3. C# - 系统类 - Math类

    Math类 ns:System 此类除了提供了最基本的运算功能 还提供了三角.指数.超常的计算 它的所有方法都是静态的 Math类的字段 E 常量e 自然对数底 值为2.71828182845905 ...

  4. ios开发时候,出现A valid provisioning profile for this executable was not found 错误

    今天一大早起来,做ios的开发,发现了一下错误:A valid provisioning profile for this executable was not found 错误的产生是在开发模式下产 ...

  5. js 字符串编码转换函数

    escape 方法 对 String 对象编码以便它们能在所有计算机上可读, escape(charString) 必选项 charstring 参数是要编码的任意 String 对象或文字. 说明 ...

  6. 安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常)

    安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常) 刚安装完后用plSql登录正常. 在dos命令行下 输入  sqlplus 用户 ...

  7. nvidia安装与卸载方式

    第1种方法:.最好的方式不是手动安装官方驱动(手动安装官方驱动无法使用gpu,而且无法启用3d,同时无法生效,所以最好采用此种方法),而是使用bumblebee-nvidia安装,不过要先添加x-sw ...

  8. HUD加载动画支持gif

    下载地址 https://pan.baidu.com/s/1c2E7QW4

  9. .NET 设计模式之单例模式(一)

    1.简单解释:在创建对象时,无论创建多少次,在堆空间上只会申请一次内存空间. 2.例子(1): public class Singleton{ private static Singleton _si ...

  10. C# 内存法图像处理

    内存法通过把图像储存在内存中进行处理,效率大大高于GetPixel方法,安全性高于指针法. 笔者当初写图像处理的时候发现网上多是用GetPixel方法实现,提到内存法的时候也没有具体实现,所以笔者在这 ...