我希望這不會來得太晚,但是對於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