2012-10-05 67 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操作;