2012-01-31 53 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