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. java18 任务调度

    任务调度; Timer类, /** 了解 Timer() schedule(TimerTask task, Date time) schedule(TimerTask task, Date first ...

  2. lscpu lsblk lsscsi lspci

    [root@server1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): -bit, -bit Byte Order: Little Endian CP ...

  3. linux上gcc

    查看gcc版本号 rpm -qa | grep gcc gnu的gcc是linux/unix下开发的,不能直接在window下运行.window下有gcc的移植版本.就是楼上说的MinGW和cygwi ...

  4. 【转】hibernate.hbm.xml详解

    在Hibernate中,各表的映射文件….hbm.xml可以通过工具生成,例如在使用MyEclipse开发时,它提供了自动生成映射文件的工具.配置文件的基本结构如下: Xml代码 <?xmlve ...

  5. 关于字符串计算size的方法比较

    往往字符串需要计算size来满足UI排版的自适应的需要,而一般字符串也是放在UILabel里的. 而在计算size的方法里,一般有两种方式(deprecated的就不说了). NSString的方法 ...

  6. RxAndroid结合Retrofit,看看谁才是最佳拍档!

    这篇博文酝酿好久了,今天终于下定决心开始写!RxAndroid和Retrofit都算是当下非常流行的Android开发框架,这两个框架光是单独使用就已经爽歪歪了,那么将RxAndroid和Retrof ...

  7. Asp.Net分页存储过程

      SQL分页语句 一.比较万能的分页: sql代码: 1 2 3 select top 每页显示的记录数 * from topic where id not in  (select top (当前的 ...

  8. Android_常用控件及适配器

    TextView 控件中显示的内容必须是文本 TextView中常用的属性 android:text TextView中显示的文本内容 android:textColor 字体颜色 格式为#RGB # ...

  9. Java public, private, protected and default

    Class       Package       Subclass    World public               y             y                 y   ...

  10. 在Windows下自动运行Modelsim

    首先声明:该文章是在刘志伟老师的<Modelsim的Tcl命令>的基础上写的,希望我们能越来越自动化. 1.编写好源文件.包含asyn_fifo.v.fifomem.v.rptr_empt ...