2010-03-25 23 views
42

有没有一种特殊的方法来获取iPhone的方向?我不需要度数或弧度,我希望它返回一个UIInterfaceOrientation对象。我只需要一个如果 - 其他建设像如何获取iPhone目前的方向?

if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
//Code 
} 
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft) { 
//Code 
} 

在此先感谢!

回答

110

这很可能是你想要什么:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 

然后可以使用系统的宏,如:

if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
{ 

} 

如果你想在设备方向使用:

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

这包括列举如UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown

+0

是的,它的工作!比你非常! – Knodel 2010-03-26 08:14:43

+29

这在技术上是错误的,你从你的方法调用中获得一个UIDeviceOrientation并将其存储在一个UIInterfaceOrientation中。 UIInterfaceOrientation是4中的一个,它代表了你的界面看起来像什么。 UIDeviceOrientation是6之一(包括平放和平躺倒置),并告诉你设备的方向,而不一定是你的界面的样子。您的界面可以处于肖像模式,但如果设备坐在桌子上,它将返回UIDeviceOrientationFaceUp。 – 2011-04-26 02:36:18

+12

@CoryImdieke:感谢您更新设备和界面方向之间的差异。 UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];是正确的API – Senthil 2011-12-22 12:08:51

11

正如在其他答案中所讨论的,您需要interfaceOrientation,而不是deviceOrientation。

最简单的方法是在你的UIViewController上使用属性interfaceOrientation。 (所以大多数情况下,只是:self.interfaceOrientation会这样做)。

可能的值有:

UIInterfaceOrientationPortrait   = UIDeviceOrientationPortrait, 
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, 
UIInterfaceOrientationLandscapeLeft  = UIDeviceOrientationLandscapeRight, 
UIInterfaceOrientationLandscapeRight  = UIDeviceOrientationLandscapeLeft 

记住: 左方向是通过翻转设备向右输入。

+0

太糟糕了,现在不推荐使用。 – Rivera 2016-04-11 15:36:51

+0

是的,这是不赞成的,我们现在做什么? – trapper 2016-12-09 07:41:26

4

这是我写的hade代码片段,因为当方向改变时,我得到了更奇怪的问题回到我的根视图中....我看到只是调出应该调用的方法,但是似乎是....这伟大工程没有任何错误

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){ 
     //do something or rather 
     [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft]; 
     NSLog(@"landscape left"); 
    } 
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){ 
     //do something or rather 
     [self 
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight]; 
     NSLog(@"landscape right"); 
    } 
    if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){ 
     //do something or rather 
     [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait]; 
     NSLog(@"portrait"); 
    } 
} 
+0

这对'UIDeviceOrientationFaceUp'和'UIDeviceOrientationFaceDown'不起作用 – trapper 2016-12-10 04:14:37

1

UIInterfaceOrientation现在已经过时,并UIDeviceOrientation包括UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown因此不能依靠它来给你的接口方向。

的解决方案是非常简单的,虽然

if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) { 
    // Landscape 
} else { 
    // Portrait 
} 
相关问题