码迷,mamicode.com
首页 > 移动开发 > 详细

iOS屏幕旋转总结

时间:2016-05-04 18:38:47      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

一、屏幕旋转检测方法

在特别的场景下,需要针对屏幕旋转作特殊处理。在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 }

 

 

 

iOS屏幕旋转总结

标签:

原文地址:http://www.cnblogs.com/HOYF/p/5459054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!