2010-08-19 74 views
9

在我的应用程序中,当用户摇动设备时会显示一个图像。横向和纵向模式有不同的图像。如果iPhone的方向是纵向或横向,它工作正常。但是,如果是UIDeviceOrientationFaceDownUIDeviceOrientationFaceUp,图像应该取决于最后使用的方向,因为该方向仍然显示视图。如何获取iPhone视图的当前方向或设备的最后方向?

如何获取设备的最后方向?

回答

26

我已经想通了!我必须检查状态栏的方向!

[UIApplication sharedApplication].statusBarOrientation 
+0

非常感谢你:)))) – cV2 2012-01-16 16:28:20

+2

如果状态栏被隐藏了怎么办? – Shailesh 2013-07-04 10:10:16

+0

伟大的兄弟谢谢! – 2016-01-28 10:25:12

2

那么在一个类的shouldAutoRotate方法中,它将传入一个名为interfaceOrientation的参数。此方法在设备方向更改时随时调用。您可以将其存储为变量并在稍后查看它以确定最后的方向。

1 = standard portrait 
2 = upsidedown 
3 = button right landscape 
4 = button left landscape. 

此外,调用self.interfaceOrientation会为您提供当前的设备方向。

希望这至少让你开始正确的方向。

+0

self.interfaceOrientation给我的只是UIDeviceOrientationFaceDown(Up),所以它没有帮助。我将尝试查明当orientation为FaceUp时,shouldAutoRotate方法的interfaceOrientation是否被覆盖。谢谢! – Knodel 2010-08-19 15:11:34