Mav*_*Bzh 8 javascript iphone html5 device-orientation gyroscope
我正在尝试在Javascript中使用HTML5 deviceOrientation事件来在用户围绕他旋转iPhone时旋转图像.
我使用此代码来检测陀螺仪何时移动:
window.addEventListener('deviceorientation', function (e) {
alpha = e.alpha;
beta = e.beta;
gamma = e.gamma;
}, true);
Run Code Online (Sandbox Code Playgroud)
它在iPhone 4+和iPad 2上运行良好,但3GS(以及较旧的iPhone)上没有陀螺仪.这就是我正在测试deviceOrientationSupport的原因:
if(window.DeviceOrientationEvent){ // gyroscope support
alert('DeviceOrientationEvent support OK');
} else {
alert('DeviceOrientationEvent support KO');
}
Run Code Online (Sandbox Code Playgroud)
我在很多网站或论坛上都看过这个代码,但我的问题是在IOS 5.0.1下用我的iPhone 3GS,这个测试表明我:deviceOrientationSupport OK!
我认为这个测试只检查Safari是否能够处理这些事件:(
所以我的问题是,是否可以添加测试以了解硬件是否可以触发定向事件?
谢谢 !
在遇到与上面相同的问题后,我也遇到了旧版 iPad/iPhone 的问题,甚至没有调用 addEventListener 上的函数。
因此,我发现以下方法对我来说效果更好,并且消除了不受支持的设备通过的任何机会(这是带有加载屏幕的,因此在完成完整测试之前确实需要一段时间间隔。所以并不适合所有人)。
var _i = null;
var _e = null;
var _c = 0;
var updateDegree = function(e){
_e = e;
}
window.addEventListener('deviceorientation', updateDegree, false);
// Check event support
_i = window.setInterval(function(){
if(_e !== null && _e.alpha !== null){
// Clear interval
clearInterval(_i);
// > Run app
}else{
_c++;
if(_c === 10){
// Clear interval
clearInterval(_i);
// > Redirect
}
}
}, 200);
Run Code Online (Sandbox Code Playgroud)