如何获得设备的方向 [英] How to get device orientation

查看:186
本文介绍了如何获得设备的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是让与的cocos2d-X(C ++)?

What is the best way to get the device orientation with cocos2d-x (c++)?

我的Andr​​oid / iOS的应用程序不支持所有4个设备取向,并且比您知道它是横向或纵向比较重要的,我想知道这是否是上下颠倒或没有。

My Android/iOS application does support all 4 device orientations, and more important than knowing if it is landscape or portrait, I would like to know if it is upside down or not.

我将不得不从CCLayer内就知道了。

I will have to know from within a CCLayer.

更新

我试图得到正确的倾斜和左倾斜事件。这样做,我计算在该设备被保持的角度。但是,我需要知道什么定位设备已,要能知道,如果我其实倾左或右。

I am trying to get events for right tilt and left tilt. To do so, I am calculating the angle at which the device is held. However, I need to know what orientation the device has, to be able to know if I am actually tilting left or right.

推荐答案

虽然的cocos2d-x是跨平台的,一些事情仍然非常依赖于平台,例如,如果你看到了CCFileUtils,我们对iOS的独立的实现文件,机器人等,跨平台保持了一个头,

Though cocos2d-x is cross platform, few things are still very much platform dependent, for example, if you see the CCFileUtils, we have separate implementation files for iOS, Android etc, the cross platform is maintained with a single header,

牢记这一点,

在Android中,您可以通过使用常量获得设备的方向,

In Android, you can get the device orientation by using the constants,

Surface.ROTATION_0 (no rotation)
Surface.ROTATION_90
Surface.ROTATION_180
Surface.ROTATION_270. 

您可以通过调用get Display.getRotation()
(只在所有这些方向旋转如果安卓screenOrientation:fullSensor 设置)

和iOS中,
你可以用

And in iOS, You can get the values with

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight

(只在所有这些方向的旋转,如果, supportedInterfaceOrientations 方法的返回值 UIInterfaceOrientationMaskAll

请注意:
它不是一个很好的做法,在纵向模式下倒挂旋转手机,只能在平板电脑的接受。(苹果公司甚至可能会拒绝您为此应用程序)

Note: Its not a good practice to rotate a phone upside down in portrait mode, only on tablets its accepted.. (Apple might even reject your app for this reason)

让,如果你想知道如何使用它我知道,但谷歌搜索的关键字会给你的使用。

Let me know if you want to know how to use it, but googling the keywords will give you the usage..

这篇关于如何获得设备的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆