zoukankan      html  css  js  c++  java
  • 横竖屏事件响应

    最近搞横竖屏,获得一些心得,特记录下来。

    做横竖屏最重要的是确定横竖屏响应的接口。目前我知道的有两种方式 :

    1.使用通知。

        

    - (void)viewDidLoad

          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_orientationDidChange:)name:UIDeviceOrientationDidChangeNotification object:nil];

    }

     

    - (void)dealloc {

     

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotificationobject:nil];

    }

     

    -(void)_orientationDidChange:(NSNotification*)notify

    {

        [self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];

    }

    -(void)_shouldRotateToOrientation:(UIDeviceOrientation)orientation {

     

    if (orientation == UIDeviceOrientationPortrait ||orientation == UIDeviceOrientationPortraitUpsideDown) {

              // 竖屏

    }

    else {

             // 横屏

    }

    }

    上述代码,一看就明白。

    2.使用  viewWillLayoutSubviews

      测试发现横竖屏切换的时候,系统会响应一些函数,其中 viewWillLayoutSubviews就是之一。

     

    - (void)viewWillLayoutSubviews

    {

         [self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];

    }

    通过上述一个函数就知道横竖屏切换的接口了。

    注意:

    viewWillLayoutSubviews只能用在ViewController里面,在view里面没有响应。

  • 相关阅读:
    [转]看懂UML类图
    [转]客户需要什么样的业务解决方案
    [转]逻辑和计算机
    [转]数据库备份与恢复方案
    125个工具与技术(PMBOK2008)
    72个可交付成果(PMBOK2008)
    47个过程(PMBOK2008)
    项目管理详细任务(PMBOK2008)
    项目管理过程组和知识领域表(PMBOK2008)
    项 目 管 理 知 识 体 系 指 南 (PMBOK2008)
  • 原文地址:https://www.cnblogs.com/huangh/p/4204239.html
Copyright © 2011-2022 走看看