读书人

UIInterfaceOrientation跟UIDeviceOri

发布时间: 2012-09-28 00:03:35 作者: rapoo

UIInterfaceOrientation和UIDeviceOrientation的区别

UIInterfaceOrientation和UIDeviceOrientation长得是又长又像,长久时间我根本没有区分这两个类型,肯定也混用了无数,直到我现在处理的一个问题,明明是横/竖屏的情况,控件却是以竖/横的布局展示,而且我在模拟器上怎么都搞不出来,而测试稍微把机器转转圈就能重现这个现象。

?

那么定位的方向很明确,肯定是在willRotateToInterfaceOrientation之类的函数里咯。于是我看到了这样的代码

typedef enum {    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft} UIInterfaceOrientation;

?也就是意味着竖屏的时候两者的值是完全对应,而横屏的时候相反的对应关系则反应了向左摇的时候,设备正好是朝着右边的,也有人说HOME在右边。

?

还有就是UIDeviceOrientation一定会根据设备当前的位置朝向而改变,但UIInterfaceOrientation则未必,比如你根本不让设备转动的话,那么怎么都是获得到UIInterfaceOrientationPortrait这个值的。

?

如果你的设备支持摇动,那么直接在willRotateToInterfaceOrientation的函数里根据参数即可获得当前状态,而万一遇到新界面没有获得摇动消息却要确定当前状态,那就用[UIApplication sharedApplication].statusBarOrientation写在viewWillAppear之类的函数里。当然最方便最合适的做法是用UIInterfaceOrientationIsPortrait和UIInterfaceOrientationIsLandscape两个预设宏。

?

?

读书人网 >移动开发

热点推荐