zoukankan      html  css  js  c++  java
  • iOS屏幕旋转总结

    一、屏幕旋转检测方法

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

    我下面介绍两种方法:

    1、监测状态栏方向

     1 /**
     2  *  当手机屏幕发生左右横屏变化时,我们需根据此时的设备方向做出相应的调整
     3  *  例如,在ViewController中,我们需要监控手机屏幕是否转动了,需要在通知中心中注册通知
     4  */
     5 - (void)viewWillAppear:(BOOL)animated{
     6     [super viewWillAppear:animated];
     7     
     8     //注册通知,监测手机屏幕变化
     9     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    10 }
    11 
    12 /**
    13  *  当手机屏幕发生变化时,会回调下面方法
    14  */
    15 - (void)statusBarOrientationChange:(NSNotification *)notification{
    16     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    17     
    18     if (orientation == UIInterfaceOrientationLandscapeRight)//home键向右
    19     {
    20         NSLog(@"home键向右");
    21     }
    22     if (
    23         orientation ==UIInterfaceOrientationLandscapeLeft) // home键靠左
    24     {
    25         NSLog(@"home键向左");
    26     }
    27     
    28     if (orientation == UIInterfaceOrientationPortrait)// home键靠下
    29     {
    30         NSLog(@"home键靠下");
    31     }
    32     
    33     if (orientation == UIInterfaceOrientationPortraitUpsideDown)// home键颠倒
    34     {
    35         NSLog(@"home键靠颠倒");
    36     }
    37 }

    Debug时我们发现下面这段代码没有被打印

    1 if (orientation == UIInterfaceOrientationPortraitUpsideDown)// home键颠倒
    2     {
    3         NSLog(@"home键靠颠倒");
    4     }

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

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

    2、监控设备物理方向

     1 - (void)viewWillAppear:(BOOL)animated{
     2     [super viewWillAppear:animated];
     3     
     4     //注册通知 监测设备方向的变化
     5     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
     6     
     7 }
     8 
     9 //当设备的物理方向发生变化时会调
    10 - (void)orientChange:(NSNotification *)noti{
    11     
    12     NSDictionary* ntfDict = [noti userInfo];
    13     
    14     UIDeviceOrientation  orient = [UIDevice currentDevice].orientation;
    15     
    16     switch (orient)
    17     {
    18         case UIDeviceOrientationPortrait:
    19             NSLog(@"home靠下");
    20             break;
    21         case UIDeviceOrientationLandscapeLeft:
    22             NSLog(@"home靠右边");
    23             
    24             break;
    25         case UIDeviceOrientationPortraitUpsideDown:
    26             NSLog(@"home靠上");
    27             break;
    28         case UIDeviceOrientationLandscapeRight:
    29             NSLog(@"home靠左边");
    30             break;
    31             
    32         default:
    33             break;
    34     }
    35 }

     

     

  • 相关阅读:
    【STM32H7教程】第35章 STM32H7的定时器应用之高精度单次延迟实现(支持TIM2,3,4和5)
    【重大更新】AppWizard来了,emWin6.10版本来了
    【STM32H7教程】第34章 STM32H7的定时器应用之TIM1-TIM17的PWM实现
    【STM32H7教程】第33章 STM32H7的定时器应用之TIM1-TIM17的中断实现
    【性能测评】DSP库,MDK5的AC5,AC6,IAR和Embedded Studio的三角函数性能
    【实战经验分享】一劳永逸的解决网线随意热插拔问题
    基于STM32的无损压缩算法miniLZO移植,压缩率很高,20KB随机数压缩到638字节,耗时275us
    Java Number & Math类
    Java switch case语句
    java条件语句
  • 原文地址:https://www.cnblogs.com/HOYF/p/5459054.html
Copyright © 2011-2022 走看看