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. systemtap 列出所有linux 内核模块与相关函数1

    阿里云主机 [root@monitor klvl]# uname -aLinux monitor 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20: ...

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

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

  3. MVC - Ajax

    Ajax表单 使用Ajax.BeginForm方法即可开启Ajax表单 Ajax.BeginForm() 参数1:Action名参数2:使用AjaxOptions实例设定异步请求的属性 new Aja ...

  4. yum在线升级

    RPM优点 由於 RPM 是透过预先编译并打包成为 RPM 文件格式后,再加以安装的一种方式,并且还能够进行数据库的记载. 所以 RPM 有以下的优点: RPM 内含已经编译过的程序与配置档等数据,可 ...

  5. tarball文件安装的大概流程

    ./configure这个步骤就是在创建 Makefile 这个文件罗!通常程序开发者会写一支 scripts 来检查你的 Linux 系统.相关的软件属性等等,这个步骤相当的重要, 因为未来你的安装 ...

  6. Java基础知识强化之IO流笔记37:FileReader/FileWriter(转换流的子类)复制文本文件案例

    1. 转换流的简化写法: 由于我们常见的操作都是使用本地默认编码,所以,不用指定编码.而转换流的名称有点长,所以,Java就提供了其子类供我们使用:FileReader / FileWriterOut ...

  7. Android(java)学习笔记152:Android运行时异常“Binary XML file line # : Error inflating class”

    在原生Android下编译APK,编译没有问题,但是在运行的时候经常出现如标题所描述的异常:"Binary XML file line # : Error inflating class&q ...

  8. HttpClient4.4 进行Http连接通讯

    以前一直使用jdk自带的urlConnection来进行http通讯,HttpClient与之相比,HttpClient更具有灵活度和易用性.HttpClient能够方便使用连接池,使用时需要重新创建 ...

  9. Spring MVC中如何传递对象参数

    springController: @Controller @RequestMapping("/user") public UserController extends BaseC ...

  10. day0

    /* 考前最后一天了 由于下午赶路 就放到上午发了 早晨浏览博客 上午浏览博客 感谢学弟为我写的博客233 很开心认识你们这一群人 嗯 最后一天了 就要说再见了 大家加油吧 ^ ^ */