如何获取iPhone当前的方向?
有没有特殊的方法来获取 iPhone 的方向?我不需要它的度数或弧度,我希望它返回一个 UIInterfaceOrientation 对象。我只需要它来进行 if-else 结构,例如
if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}
提前致谢!
Is there a special method to get iPhones orientation? I don't need it in degrees or radians, I want it to return an UIInterfaceOrientation object. I just need it for an if-else construction like
if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很可能是您想要的:
然后您可以使用系统宏,例如:
如果您想要设备方向使用:
这包括诸如
UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
之类的枚举This is most likely what you want:
You can then use system macros like:
If you want the device orientation use:
This includes enumerations like
UIDeviceOrientationFaceUp
andUIDeviceOrientationFaceDown
正如其他答案中所讨论的,您需要 interfaceOrientation,而不是 deviceOrientation。
最简单的方法是使用 UIViewController 上的 interfaceOrientation 属性。 (所以大多数情况下,只需: self.interfaceOrientation 就可以了)。
可能的值有:
请记住:
通过将设备向右转动即可进入左方向。
As discussed in other answers, you need the interfaceOrientation, not the deviceOrientation.
The easiest way to get to this, is to use the property interfaceOrientation on your UIViewController. (So most often, just: self.interfaceOrientation will do).
Possible values are:
Remember:
Left orientation is entered by turning your device to the right.
这是我必须编写的一段代码,因为当方向改变时,我的根视图中出现了奇怪的问题......我你看到只是调用了应该被调用但似乎确实是的方法。 ...这很好用,没有错误
Here is a snippet of code I hade to write because I was getting wierd issues coming back into my root view when the orientation was changed .... I you see just call out the method that should have been called but did seem to be .... this works great no errors
UIInterfaceOrientation
现已弃用,UIDeviceOrientation
包括UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
,因此不能依赖为您提供界面方向。解决方案虽然很简单
UIInterfaceOrientation
is now deprecated, andUIDeviceOrientation
includesUIDeviceOrientationFaceUp
andUIDeviceOrientationFaceDown
so can't be relied on to give you the interface orientation.The solution is pretty simple though
随着
[UIApplication statusBarOrientation]
被弃用,您现在应该使用:With
[UIApplication statusBarOrientation]
being deprecated you should now use: