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

摘要:
在ios系统下实现相关的功能还是比较方便的。当我们关注的东西和布局相关而不是纯粹设备旋
  1. -(void)rotation_icon:(float)n{
  2. UIButton*history_btn=[self.viewviewWithTag:<#(NSInteger)#>][self.viewviewWithTagName:@"home_history"];
  3. UIButton*cam_btn=[self.viewviewWithTagName:@"cam_btn"];UIButton*cut_btn=[self.viewviewWithTagName:@"cut_btn"];UIButton*light_btn=[self.viewviewWithTagName:@"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{UIDeviceOrientationo=[[UIDevicecurrentDevice]orientation];
  10. switch(o){
  11. caseUIDeviceOrientationPortrait://Deviceorientedvertically,homebuttononthebottom
  12. [selfrotation_icon:0.0];
  13. break;
  14. caseUIDeviceOrientationPortraitUpsideDown://Deviceorientedvertically,homebuttononthetop
  15. [selfrotation_icon:180.0];
  16. break;
  17. caseUIDeviceOrientationLandscapeLeft://Deviceorientedhorizontally,homebuttonontheright
  18. [[UIApplicationsharedApplication]setStatusBarOrientation:UIInterfaceOrientationLandscapeRightanimated:YES];
  19. [selfrotation_icon:90.0*3];
  20. break;
  21. caseUIDeviceOrientationLandscapeRight://Deviceorientedhorizontally,homebuttonontheleft
  22. [[UIApplicationsharedApplication]setStatusBarOrientation:UIInterfaceOrientationLandscapeLeftanimated:YES];
  23. [selfrotation_icon:90.0];
  24. break;
  25. default:
  26. break;
  27. }
  28. }
  29. -(void)viewWillDisappear:(BOOL)animated{
  30. NSNotificationCenter*nc=[NSNotificationCenterdefaultCenter];
  31. UIDevice*device=[UIDevicecurrentDevice];//Getthedeviceobject
  32. [ncremoveObserver:selfname:UIDeviceOrientationDidChangeNotificationobject:device];
  33. }
  34. -(void)viewDidAppear:(BOOL)animated{
  35. //Doanyadditionalsetupafterloadingtheviewfromitsnib.
  36. //-----SETUPDEVICEORIENTATIONCHANGENOTIFICATION-----
  37. UIDevice*device=[UIDevicecurrentDevice];//Getthedeviceobject
  38. [devicebeginGeneratingDeviceOrientationNotifications];//Tellittostartmonitoringtheaccelerometerfororientation
  39. NSNotificationCenter*nc=[NSNotificationCenterdefaultCenter];//Getthenotificationcentrefortheapp
  40. [ncaddObserver:selfselector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotificationobject:device];
======================
iPad iPhone 屏幕旋转检测的方法

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

我下面介绍两种方法:

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

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(statusBarOrientationChange:)name:UIApplicationDidChangeStatusBarOrientationNotificationobject:nil];

- (void)statusBarOrientationChange:(NSNotification*)notification

{

UIInterfaceOrientationorientation = [[UIApplicationsharedApplication]statusBarOrientation];

if(orientation ==UIInterfaceOrientationLandscapeRight)// home键靠右

{

//

}

if(

orientation ==UIInterfaceOrientationLandscapeLeft)// home键靠左

{

//

}

if(orientation ==UIInterfaceOrientationPortrait)

{

//

}

if(orientation ==UIInterfaceOrientationPortraitUpsideDown)

{

//

}

}

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

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

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

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(orientChange:)name:UIDeviceOrientationDidChangeNotificationobject:nil];

- (void)orientChange:(NSNotification*)noti

{

NSDictionary* ntfDict = [notiuserInfo];

UIDeviceOrientationorient = [UIDevicecurrentDevice].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)

{

caseUIDeviceOrientationPortrait:

break;

caseUIDeviceOrientationLandscapeLeft:

break;

caseUIDeviceOrientationPortraitUpsideDown:

break;

caseUIDeviceOrientationLandscapeRight:

break;

default:

break;

}

}

注意到这种方式里面的方向还包括朝上或者朝下,很容易看出这个完全是根据设备自身的物理方向得来的,当我们关注的只是物理朝向时,我们通常需要注册该通知来解决问题(另外还有一个加速计的api,可以实现类似的功能,该api较底层,在上面两个方法能够解决问题的情况下建议不要用,使用不当性能损耗非常大)。
==================================================
最近在ipad上面调试程序的时候 ,虽然 去掉了 屏幕旋转的那俩钩钩,但是在ipadmini 上 还是 会 旋转,很奇怪,ipad系统是 ios12 用 xcode10开发的程序。
监听iOS检测屏幕旋转状态,不需开启屏幕旋转第1张
解决方案

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
参考链接 https://blog.csdn.net/nadeal/article/details/79542682

免责声明:文章转载自《监听iOS检测屏幕旋转状态,不需开启屏幕旋转》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇LoadRunner11遇到问题及解决办法再见,Java 8!Java 17 终于免费了,史上最快的 JDK。。下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

IOS5,6,7不同版的适配. 强制旋转和自动旋转.

改变Orientation的三种途径 这里, 咱们主要理清一下: 到底有哪些设置可以改变屏幕旋转特性. 这样: 出现任何问题我们都可以从这几个途径中发现原因. 灵活应付产品经理的各种需求. 首先我们得知道: 当手机的重力感应打开的时候, 如果用户旋转手机, 系统会抛发UIDeviceOrientationDidChangeNotification ...

移动端——JS(一)

javascript(简称js)语言在移动前端可以说必不可少,许多效果都是和js相关的,包括现在移动端的一些框架:jqmobi、jqtouch、sencha touch、jquerymobile等等。都是基于js编写的。 暂时不讨论这些框架,主要讨论一下部分常用的js事件在移动端的使用。举几个例子: 1、隐藏地址导航栏: <script> /...

ROS惯导数据发布(Python)

一、背景   基本配置:ubuntu 16.04,ROS Kinetic   惯导型号:维特智能 WT61C(六轴惯导)   维特智能官方提供的参考程序是通过手动比较各个字节来确定数据包/数据帧的,个人认为比较繁琐,因此采用Python的re(正则表达式)和struct(字节处理)模块简化其数据匹配和提取,并实现惯导数据在ROS中的发布。 二、程序   惯...

Android屏幕重力感应旋转

public class MainActivity extends AppCompatActivity {   private MyOrientoinListener myOrientoinListener; @Override protected void onCreate(Bundle savedInstanceState) {...

Android开发 设备横屏与竖屏的详解

需要了解横竖屏切换关键知识  1.在Android设备的横竖屏幕,每一次切换横竖屏其实是在重新创建Activity,Activity会重新走一遍生命周期.从onCreate 到 onDestroy   2.在Activity类里的变量也会重新创建,这点需要注意! 判断屏幕方向,方式一判断屏幕是否竖屏   @Override protected vo...

基于HTML5的iPad电子杂志横竖屏自适应方案

基于HTML5的iPad电子杂志横竖屏自适应方案 (转载自:http://www.yeeach.com/?p=1172) 基于HTML5来制作iPad电子杂志,横屏及竖屏自适应是个大问题,查找了半天资料,没有一篇像样的文章可供参考。将思路及例子分享一下。例子并不严谨和规范,仅供参考。 大致思路: 1、对横屏(portrait)和竖屏(landscape)情...