2012-10-05 26 views
-1

你好,我有一个情况,我想调用不同的方向(Potrait和风景)单独的方法。 因此,任何人都可以帮助我。IOS中屏幕方位和风景方向(目标C)

例如

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

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

回答

1

我用[[UIApplication sharedApplication] statusBarOrientation]知道方向。

然后我用这个方法

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

不使用[[UIDevice currentDevice] orientation],因为如果该设备是在桌子上,例如,它不能正常工作。

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

枚举是UIDeviceOrientation[something]

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

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

UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait是宏的。

+0

这不是最好的,因为有'UIDeviceOrientationFaceUp'和'UIDeviceOrientationFaceDown' – Vahan

0

尝试:

UIInterfaceOrientation interfaceOrientation = [[UIApplication的sharedApplication] statusBarOrientation];

如果(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)

{// perform ua operation for landscape mode 

}否则{

//执行对肖像模式 } UA操作;