处理的iOS设备方向(handling ios device orientation)

2019-09-18 04:46发布

我取决于设备的方向有问题与我uiviews ...

那我遇到的主要问题是,

UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown

我的观点是搞乱我只是想支持纵向和横向(左,右),所以如果设备改变方向我认为正确的改变本身..

这是我目前已经实现。 Basicly其从屏幕的底部向上滚动,并且具有几个按钮在该视图中,用户能够选择要加载的不同视图一个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。 如果设备面朝上或面朝下翻转发生时,所有的这些按钮看法消失..我能做些什么来阻止这一点,任何帮助,将gretly赞赏。

Answer 1:

使用UIInterfaceDirection代替。 它的一部分UIApplication类,它仅包含下列值

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

您可以轻松地检查你当前的方向是这样的:

[[UIApplication sharedApplication] statusBarOrientation] 


文章来源: handling ios device orientation