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 }

     

     

  • 相关阅读:
    node入门(一)——安装
    移动web开发基础(二)——viewport
    移动web开发基础(一)——像素
    关于min-height:100%的解决办法
    用类与原型写一个组件(三)——学习笔记
    用类与原型写一个组件(二)——学习笔记
    用类与原型写一个组件(一)——学习笔记
    js类、原型——学习笔记
    Android 常用RGB值及名称
    AES加密示例
  • 原文地址:https://www.cnblogs.com/HOYF/p/5459054.html
Copyright © 2011-2022 走看看