iOS中纵向和横向的屏幕方向(Objective-C)

时间:2012-10-05 09:23:35

标签: objective-c ios orientation

我有一种情况,我想为不同的方向(人像和风景)调用单独的方法。

例如:

If (Orientation == Portrait)
{
  Some method a;
}

Elseif (Orientation == Landscape)
{
  Some method b;
}

5 个答案:

答案 0 :(得分:1)

我使用[[UIApplication sharedApplication] statusBarOrientation]来了解方向。

然后我使用这个方法

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Do something if landscape
} else {
//Do something in portrait
}

请勿使用[[UIDevice currentDevice] orientation],因为如果设备位于桌面上,它无法正常工作。

答案 1 :(得分:1)

    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
         // code for landscape orientation      
    }

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
         // code for Portrait orientation       
    }

UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait是宏。

答案 2 :(得分:0)

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if(deviceOrientation == UIDeviceOrientationPortrait)
    ...
else  ...

枚举是UIDeviceOrientation[something]

答案 3 :(得分:0)

尝试这个:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft             || interfaceOrientation == UIInterfaceOrientationLandscapeRight)

    {// perform ua operation for landscape mode

}其他{

//执行纵向模式的操作 };

答案 4 :(得分:0)

Objective-c:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    if (UIDevice.currentDevice.orientation == UIInterfaceOrientationPortrait) {
        printf("Portrait");
    } else {
        printf("Landscape");
    }
}