【问题标题】:Detecting device orientation in cordova在科尔多瓦检测设备方向
【发布时间】:2017-09-04 02:32:43
【问题描述】:

有没有办法通过访问加速度计、指南针或其他方式来检测 Cordova 中的物理设备方向?

我想检测设备是侧向的(即横向)还是直立的(即纵向)。除了:cordova detecting orientation change 我将无法通过window.orientation 使用方向,因为如果用户锁定了他们的方向,它会失败。

我不需要知道用户所处的模式。只需要知道设备是横着还是竖着。

【问题讨论】:

标签: javascript android ios cordova phonegap-plugins


【解决方案1】:

我找到了一个描述how device orientation can be read from accelerometer despite a locked device orientation 的博客条目。

相关代码sn-p在Objective-C中提供,但自然适用于Phonegap,因为它可以通过Cordova's Device Motion Plugin进行翻译和使用。

这是原文:

float x = -[acceleration x];
float y = [acceleration y];
float angle = atan2(y, x);

if(angle >= −2.25 && angle <= −0.75) {
   //OrientationPortrait
} else if(angle >= −0.75 && angle <= 0.75){
   //OrientationLandscapeRight
} else if(angle >= 0.75 && angle <= 2.25) {
   //OrientationPortraitUpsideDown
} else if(angle <= −2.25 || angle >= 2.25) {
   //OrientationLandscapeLeft];
}

解释:对于任何不都等于0的实参x和y,atan2(y, x)是平面的正x轴和点之间的弧度角由指定的坐标给出就可以了。逆时针角度为正,顺时针角度为负。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多