我希望这不会来得太晚,但是对于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设备上测试过它。
应该指出,每个事件返回不同的数字,所以你可能需要做一些工作来规范化它们。但这是你如何访问它们的基础知识。
让我知道这是否有帮助!
您好,我只能检测设备** **运动赛事与3GS!上面编写的代码与DeviceOrientationEvent Listener一起使用可以很好地与iPhone 4和iPad 2配合使用,但在iPhone 3GS上无法使用。 – MavBzh 2012-02-01 08:46:53