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. JavaScript引擎的工作原理

    http://my.oschina.net/fuckBAT/blog/318355?fromerr=jK6wCh1p#OSC_h4_4

  2. C++ ORM ODB 入门介绍(二)

    目录[-] 1. ODB中的继承类型 2. abstract和polymorphic的区别 3.polymorphic表格 4.早期版本如何实现polymorphic 5.实例 本节主要介绍ODB中的 ...

  3. phpcms 源码分析七: 模板引擎实现

    这次是逆雪寒对模板引擎实现的分析: 1 /* 函数 template函数是在global.func.php 里面定义的. 在前面的phpcms 的首页 index.php 里就见到了. 用法: inc ...

  4. linux 配置 Apache mysql php最新版

    第一部分:安装mysql 官方下载 mysql5.6.19 64位的rpm格式文件 0.rpm 四个mysql5.6.19 卸载默认的mysql yum -y remove mysql-libs-* ...

  5. POJ1329题

    Circle Through Three Points Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Jav ...

  6. UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte

    1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...

  7. 【排障】每次打开word都提示要安装配置

    为什么每次打开word都提示要安装配置?很多人在打开word时,总是提示要安装配置一遍,花去不少时间,这是由于电脑里有两个不同版本的office软件,产生的原因可能是原来的卸载了没卸载干净,或是安装了 ...

  8. js基础1

    一.JavaScript 不同于Java 有三部分组成 核心(ECMAScript) 文档对象模型(DOM) 浏览器对象模型(BOM) 二.var 是定义数据前加的前缀   三.弹出 alert( ) ...

  9. 20151207jquery 学习笔记 Ajax

    .load()方法是局部方法,因为他需要一个包含元素的 jQuery 对象作为前缀.而$.get()和 $.post()是全局方法,无须指定某个元素.对于用途而言,.load()适合做静态文件的异步获 ...

  10. ZeroMemory

    ZeroMemory: 用0填充一个内存块 void ZeroMemory( [in] PVOID Destination, //内存块开始地址 [in] SIZE_T Length //填充块大小 ...