2012-01-31 50 views
8

我试图在用户在他周围旋转iPhone时使用Javascript中的HTML5 deviceOrientation事件来旋转图像。在Javascript中为iPhone 3GS的设备定位事件测试硬件支持

我用这个代码时,陀螺仪运动检测:

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

它确实效果很好的iPhone 4+和iPad 2,但有一个在3GS(及以上的iPhone)没有陀螺仪。这就是为什么我测试这样的deviceOrientationSupport:

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

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

我认为,这个测试检查只有在Safari浏览器能够处理这些事件:(

所以我的问题是,是否有可能增加一个测试,以了解是否可以将该硬件触发方向事件?

谢谢!

回答

3

在发生同样的问题后,我也遇到了旧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); 
0

虽然没有对3GS没有陀螺仪,它是完全能够检测设备的方向使用它已经建立了加速度的变化。iOS的。从那以后,第一代iPhone这种支持。

+0

您好,我只能检测设备** **运动赛事与3GS!上面编写的代码与DeviceOrientationEvent Listener一起使用可以很好地与iPhone 4和iPad 2配合使用,但在iPhone 3GS上无法使用。 – MavBzh 2012-02-01 08:46:53

4

我希望这不会来得太晚,但是对于iOS 4.2或更高版本的Safari,您是否会在iPhone 3GS(或其他没有陀螺仪的设备)上注册DeviceOrientationEvent?

底线,DeviceOrientation不适用于iPhone 3GS。但正如有人提到的那样,DeviceMotionEvent可以工作,但是您必须以不同于使用陀螺仪的设备访问事件数据(我知道这很愚蠢!)。

第一步:我在混合中添加了一个变量来捕获OrientationEvent是否真正用任何非空数据触发(如果有陀螺仪的话)。

var canHandleOrientation; 
if (window.DeviceOrientationEvent) { 
    window.addEventListener("deviceorientation", handleOrientation, false); 
} 

function handleOrientation(event){ 
    console.log("Orientation:" + event.alpha + ", " + event.beta + ", " + event.gamma); 
    canHandleOrientation = event; // will be either null or with event data 
} 

现在你知道这个事件是否真的有陀螺仪数据!所以,如果你想默认其他东西(例如window.DeviceMotionEvent),你可以使用条件。

if (!canHandleOrientation) { 
    console.log("There is no gyroscope") 
} 

我在我的MacBook Pro(陀螺仪)测试了在移动Safari浏览器为iPhone 3GS(无陀螺仪)和iPad 2(陀螺仪)和Chrome。似乎工作。现在

,如果你想获得DeviceMotionEvent数据作为选择,如果该方位数据不可用,那么......

if (window.DeviceMotionEvent && !canHandleOrientation) { 
    window.addEventListener('devicemotion', handleMotion, false); 
} 

function handleMotion(event){ 
    if(event.acceleration){ 
    //requires a gyroscope to work. 
    console.log("Motion Acceleration: " + event.acceleration.x + ", " + event.acceleration.y + ", " + event.acceleration.z); 
    } 
    else{ 
    //this is for iPhone 3GS or a device with no gyroscope, and includes gravity. 
    console.log("Motion AccelerationGravity: " + event.accelerationIncludingGravity.x + ", " + event.accelerationIncludingGravity.y + ", " + event.accelerationIncludingGravity.z); 
    } 
} 

这应包括你的基地与WebKit浏览器的大多数设备.. 。 我希望。没有在任何Android设备上测试过它。

应该指出,每个事件返回不同的数字,所以你可能需要做一些工作来规范化它们。但这是你如何访问它们的基础知识。

让我知道这是否有帮助!

+0

嗨阿琼!我只想感谢你,因为你在答案中给了我真正有用的信息。我希望这篇文章能够帮助其他开发人员在Safari中面对这种棘手的行为。 – MavBzh 2012-04-19 09:49:35

0

我有一个类似的问题,我的桌面也接受了window.DeviceOrientationEvent事件。我用下面的代码在移动设备检查方向支持:

var deviceSupportOrientation = false; 
window.onload = function() { 
    if (window.DeviceOrientationEvent) { 
     window.addEventListener("deviceorientation", function() { 
      deviceSupportOrientation = true; 
      window.removeEventListener('deviceorientation'); 
     }, false); 
    } 
}; 

的jsfiddle:http://jsfiddle.net/7L5mg8hj/1/]

+0

在S4手机和S4平板电脑上测试了这款小提琴。在平板电脑上,设备方向数据始终为'0',这意味着该事件是受支持的,但没有给出数据,这意味着它通过不支持'deviceorientation'滑动。小心那里。 – lowtechsun 2016-01-31 16:29:33