如何在Cordova中检测设备旋转?
我想用Cordova检测实时设备旋转(不是方向,旋转角度)。我使用了设备运动,但我得到了一系列加速度x y和z值,我如何从中计算旋转 我想检测一个360度的旋转(想象一下将设备放在一根棍子(或钟摆)上,然后敲击它使其摆动) 谢谢 你可以安装甘地在评论中提到的如何在Cordova中检测设备旋转?,cordova,phonegap-plugins,cordova-plugins,phonegap,Cordova,Phonegap Plugins,Cordova Plugins,Phonegap,我想用Cordova检测实时设备旋转(不是方向,旋转角度)。我使用了设备运动,但我得到了一系列加速度x y和z值,我如何从中计算旋转 我想检测一个360度的旋转(想象一下将设备放在一根棍子(或钟摆)上,然后敲击它使其摆动) 谢谢 你可以安装甘地在评论中提到的 cordova插件添加cordova插件屏幕方向 然后,您可以使用事件来检测devicerady之后的方向变化 window.addEventListener('orientationchange', function(){ con
cordova插件添加cordova插件屏幕方向
然后,您可以使用事件来检测devicerady
之后的方向变化
window.addEventListener('orientationchange', function(){
console.log(screen.orientation.type); // e.g. portrait
});
或
老实说,在没有插件的情况下,您应该能够做到这一点,但添加cordova插件会更安全
当你想要检测一个360度旋转时,你需要自己数一数。。。沿着……的路线的东西
var startingPoint = 0;
window.addEventListener('orientationchange', monitorOrientation);
function monitorOrientation(e) {
console.log('Device orientation is: ', e. orientation);
if(e. orientation == 180 || e.orientation == -180) {
// You can extend this or remove it completely and increment the variable as you wish i.e. 4x90 = 360
startingPoint += 180;
}
if(startingPoint == 360) {
// Do whatever you want and reset starting point back to 0
startingPoint = 0;
}
}
也许你要找的是指南针: 以度为单位提供设备旋转的详细信息
但是,如果你正在寻找检测设备运动,这是我能看到的唯一一个接近这一要求的插件。但设备旋转计数应该使用自定义逻辑进行跟踪,该逻辑需要建立在x、y、z坐标之上。插件中还没有现成的解决方案,据我所查您使用加速度x y和z值计算设备的“动态旋转” 使用钟摆类比,假设您确定设备将始终面向用户左右摆动(即沿x轴移动),您可以使用x值
请记住,您不会获得学位值。上次我检查时,当从一个横向旋转到另一个横向时,iOS返回0表示直立(纵向),然后返回-9到9。此插件以度为单位提供设备旋转的详细信息。这就是你想要的吗?嘿@Ghandi,请看一下我给Devrim留下的评论。现在就知道了。仍然不确定是否还有其他设备运动插件可用。。。Will check thoughdevice motion是我能看到的唯一接近此要求的插件。但设备旋转计数应该使用自定义逻辑进行跟踪,该逻辑需要建立在x、y、z坐标之上。据我所知,插件中还没有现成的解决方案。谢谢,@Devrim,但设备根本不旋转。它站着。假设设备在横向模式下粘在一根棍子上,然后旋转棍子。在这种情况下,定向不会触发,这就是我正在寻找的情况。嗯,老实说,我不确定@trueicecold。我想你正在寻找设备运动或传感器工具。玩一玩这个插件是的,我看到了,但我不知道如何正常化x,y,z来检测360度的摆动。。。
var startingPoint = 0;
window.addEventListener('orientationchange', monitorOrientation);
function monitorOrientation(e) {
console.log('Device orientation is: ', e. orientation);
if(e. orientation == 180 || e.orientation == -180) {
// You can extend this or remove it completely and increment the variable as you wish i.e. 4x90 = 360
startingPoint += 180;
}
if(startingPoint == 360) {
// Do whatever you want and reset starting point back to 0
startingPoint = 0;
}
}