在Javascript中测试硬件支持,以获取iPhone 3GS的设备定位事件 [英] Testing hardware support in Javascript for device orientation events of the iPhone 3GS

查看:164
本文介绍了在Javascript中测试硬件支持,以获取iPhone 3GS的设备定位事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Javascript中使用HTML5 deviceOrientation事件来在用户围绕他旋转iPhone时旋转图像。

I'm trying to use HTML5 deviceOrientation events in Javascript to rotate an image when the user rotate his iPhone around him.

我使用此代码来检测何时陀螺仪正在移动:

I use this code to detect when the gyroscope is moving :

window.addEventListener('deviceorientation', function (e) {
    alpha = e.alpha;
    beta = e.beta;
    gamma = e.gamma;            
}, true);

它在iPhone 4+和iPad 2上运行良好,但3GS上没有陀螺仪(和老款iPhone)。这就是我正在测试deviceOrientationSupport的原因:

It really works well on iPhone 4+ and iPad 2, but there's no gyroscope on the 3GS (and older iPhones). That's why I'm testing the deviceOrientationSupport like this :

if(window.DeviceOrientationEvent){ // gyroscope support
    alert('DeviceOrientationEvent support OK');
} else {
    alert('DeviceOrientationEvent support KO');
}

我在很多网站或论坛上看过这个代码,但我的问题是在IOS 5.0.1下使用我的iPhone 3GS,这个测试表明我:deviceOrientationSupport OK!

I've seen this code on many websites or forums, but my problem is that with my iPhone 3GS under IOS 5.0.1, this test indicates me : deviceOrientationSupport OK !

我认为只有Safari能够处理这些事件时才进行此测试: (

I think that this test check only if Safari is able to handle these events :(

所以我的问题是,是否可以添加测试以了解硬件是否可以触发定位事件?

谢谢!

推荐答案

遇到与上述相同的问题后,我也遇到了问题旧的iPad / iPhone甚至没有在addEventListener上调用该函数。

After having the same issue as above I also had problems with the older iPad/iPhones not even calling the function on addEventListener.

所以我发现以下对我来说效果更好,并且消除了任何不支持的设备通过的可能性(这是一个加载屏幕所以它确实需要一个间隔才能完成它的完整测试。所以不是每个人都有。)

So I found that the following worked a lot better for me and eliminated any chance of an unsupported device getting through (this was sat with a load screen so it did require an interval before completing its full test. So not for everyone).

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);

这篇关于在Javascript中测试硬件支持,以获取iPhone 3GS的设备定位事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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