简体   繁体   English

如何获取iPhone视图的当前方向或设备的最后方向?

[英]How to get the iPhone view's current orientation or the device's last orientation?

In my app an image appears when user shakes the device. 在我的应用程序中,当用户摇动设备时会出现一个图像。 There are different images for landscape and portrait mode. 横向和纵向模式有不同的图像。 If the iPhone's orientation is portrait or landscape , it works fine. 如果iPhone的方向是纵向或横向 ,它可以正常工作。 But if it is UIDeviceOrientationFaceDown or UIDeviceOrientationFaceUp , the image should depend on the last orientation used, because the view is still displayed for this orientation. 但如果是UIDeviceOrientationFaceDownUIDeviceOrientationFaceUp ,则图像应取决于最后使用的方向,因为仍然会显示此方向的视图。

How can I get the last orientation of the device? 如何获得设备的最后定位?

I've figured it out! 我已经明白了! I had to check the status bar's orientation! 我必须检查状态栏的方向!

[UIApplication sharedApplication].statusBarOrientation

Well in the shouldAutoRotate method of a class it passes in a parameter called interfaceOrientation. 好吧,在类的shouldAutoRotate方法中,它传入一个名为interfaceOrientation的参数。 This method is called anytime the device orientation changes. 只要设备方向发生变化,就会调用此方法。 You can store this as a variable and the look at it at a later time to determine the last orientation. 您可以将其存储为变量,稍后再查看它以确定最后一个方向。

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

Also the call of self.interfaceOrientation will give you the current device orientation. 此外,self.interfaceOrientation的调用将为您提供当前的设备方向。

Hope this at least get you started in the correct direction. 希望这至少可以帮助您开始正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM