ios(ipad,iphone)屏幕旋转检测通用方法

来源:互联网 发布:如何做好淘宝客服 编辑:程序博客网 时间:2024/06/09 22:58

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

我下面介绍两种方法:

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

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

- (void)statusBarOrientationChange:(NSNotification *)notification

{

    

    UIInterfaceOrientation orientation = [[UIApplicationsharedApplication] statusBarOrientation];

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

    {

        //

    }

    

    if (

        orientation ==UIInterfaceOrientationLandscapeLeft)// home键靠左

    {

        //

    }

    

    if (orientation ==UIInterfaceOrientationPortrait)

    {

        //

    }


    if (orientation ==UIInterfaceOrientationPortraitUpsideDown)

    {

        //

    }

}

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

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

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

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

- (void)orientChange:(NSNotification *)noti

{

    

    NSDictionary* ntfDict = [noti userInfo];

    

    UIDeviceOrientation  orient = [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较底层,在上面两个方法能够解决问题的情况下建议不要用,使用不当性能损耗非常大)。




2 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩一年级不自觉做作业怎么办 小孩会读不会写怎么办 好多字都不会写怎么办 写作业怎么办才能写快 五周宝宝爱玩不写字怎么办 爱玩手机的小孩怎么办? 一年级学生记不住生字怎么办 一年级小孩记不住生字怎么办 配镜度数高了怎么办 宝宝两岁半不肯坐马桶拉臭臭怎么办 儿子字写得不好 怎么办 小孩不听话不爱读书和写字怎么办 两岁宝宝不愿意穿衣服怎么办 做题粗心不认真怎么办 5岁宝宝不会写字怎么办 四岁宝宝不会写字怎么办 4岁宝宝不写字怎么办 四岁宝宝不写字怎么办 孩子学习粗心计算能力差怎么办 一年级的小朋友不爱看书怎么办 马上要生了害怕怎么办 孩子做题不爱读题怎么办 孩子作业写的慢怎么办 孩子学习不好怎么办我们有绝招 英语不会做题怎么办呢? 小学二年级孩子厌学怎么办 狗狗拉肚子不吃东西怎么办 小孩做作业时容易发呆怎么办 一上高速就犯困怎么办 孩子初中数学学不好怎么办 高三注意力不集中怎么办 考砸了家长打我怎么办? 高三学生困疲劳怎么办 高三晚上很困怎么办 孩子上高三压力大不想上学怎么办 高三的孩子压力大怎么办 高三复读压力大怎么办 孩子一年级做数学粗心怎么办 一年级的孩子数学总粗心怎么办 天生手脚笨的人怎么办 高三的孩子厌学怎么办