处理iOS设备方向

3
我在处理UI视图时遇到了与设备方向有关的问题...
我遇到的主要问题是:
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown

他在说:“我的视图被搞乱了,我只想支持纵向和横向(左右)屏幕方向,所以如果设备改变方向,我的视图会自动正确调整。目前我已经实现了这个功能。基本上,它是一个UIView,从屏幕底部滚动上来,里面有几个按钮供用户选择加载不同的视图。”
#pragma jumpBarButtons
- (void)jumpBarButtonPosition
{

    //orientation stuff
    if (jumpBar.isViewLoaded) {

        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];


        switch (orientation) {
            case UIDeviceOrientationPortrait:
            {
                NSLog(@"Portrait");

                // Row one
                jumpButton1.frame = CGRectMake(18.45, 23.0, 57.0, 57.0);
                jumpButton2.frame = CGRectMake(93.9, 23.0, 57.0, 57.0);
                jumpButton3.frame = CGRectMake(169.35, 23.0, 57.0, 57.0);
                jumpButton4.frame = CGRectMake(244.8, 23.0, 57.0, 57.0);
                // Row tow
                jumpButton5.frame = CGRectMake(18.45, 95.0, 57.0, 57.0);
                jumpButton6.frame = CGRectMake(93.9, 95.0, 57.0, 57.0);
                jumpButton7.frame = CGRectMake(169.35, 95.0, 57.0, 57.0);
                jumpButton8.frame = CGRectMake(244.8, 95.0, 57.0, 57.0);
                // Row three
                jumpButton9.frame = CGRectMake(18.45, 167.0, 57.0, 57.0);
                jumpButton10.frame = CGRectMake(93.9, 167.0, 57.0, 57.0);
                jumpButton11.frame = CGRectMake(169.35, 167.0, 57.0, 57.0);
                jumpButton12.frame = CGRectMake(244.8, 167.0, 57.0, 57.0);
                // Row four
                jumpButton13.frame = CGRectMake(18.45, 239.0, 57.0, 57.0);
                jumpButton14.frame = CGRectMake(93.9, 239.0, 57.0, 57.0);
                jumpButton15.frame = CGRectMake(169.35, 239.0, 57.0, 57.0);
                jumpButton16.frame = CGRectMake(244.8, 239.0, 57.0, 57.0);
            }
                break;

case UIDeviceOrientationLandscapeLeft:
            case UIDeviceOrientationLandscapeRight:
            {
                viewSpaceHeight = 207;
                viewSpaceWidth = 480;

                // Row one
                jumpButton1.frame = CGRectMake(19.7, 9.0, 57.0, 57.0);
                jumpButton2.frame = CGRectMake(96.42, 9.0, 57.0, 57.0);
                jumpButton3.frame = CGRectMake(173.13, 9.0, 57.0, 57.0);
                jumpButton4.frame = CGRectMake(249.84, 9.0, 57.0, 57.0);
                jumpButton5.frame = CGRectMake(326.55, 9.0, 57.0, 57.0);
                jumpButton6.frame = CGRectMake(403.26, 9.0, 57.0, 57.0);
                // Row tow
                jumpButton7.frame = CGRectMake(19.7, 75.0, 57.0, 57.0);
                jumpButton8.frame = CGRectMake(96.42, 75.0, 57.0, 57.0);
                jumpButton9.frame = CGRectMake(173.13, 75.0, 57.0, 57.0);
                jumpButton10.frame = CGRectMake(249.84, 75.0, 57.0, 57.0);
                jumpButton11.frame = CGRectMake(326.55, 75.0, 57.0, 57.0);
                jumpButton12.frame = CGRectMake(403.26, 75.0, 57.0, 57.0);
                // Row three
                jumpButton13.frame = CGRectMake(19.7, 141.0, 57.0, 57.0);
                jumpButton14.frame = CGRectMake(96.42, 141.0, 57.0, 57.0);
                jumpButton15.frame = CGRectMake(173.13, 141.0, 57.0, 57.0);
                jumpButton16.frame = CGRectMake(249.84, 141.0, 57.0, 57.0);

            }
                break;
//..

这大致就是情况,如果竖屏显示,则有16个4,4,4,4的图标,如果横屏则有6,6,4个图标。如果设备被翻转到正面或反面,所有的按钮视图都会消失...我该怎么做才能防止这种情况发生呢?任何帮助都将不胜感激。
1个回答

3
使用 UIInterfaceDirection 代替。它是 UIApplication 类的一部分,仅包含以下值。
typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

您可以像这样轻松检查当前的方向:

[[UIApplication sharedApplication] statusBarOrientation] 

啊,太完美了!非常感谢你...我以为我疯了一会儿!:P - HurkNburkS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接