phonegap3.4摇一摇功能问题
var watchShakeID = null;var oldValue = {
x: null,
y: null,
z: null
};
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
//app.receivedEvent('deviceready');
goto_StartWatchShake();
}//,
// Update DOM on a Received Event
/*receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}*/
};
function goto_StopWatchShake() {
if (watchShakeID) {
navigator.accelerometer.clearWatch(watchShakeID);
watchShakeID = null;
}
}
function goto_StartWatchShake() {
var options = { frequency: 3000 };
watchShakeID = navigator.accelerometer.watchAcceleration(onWatchShakeSuccess, onWatchShakeError, options);
alert(watchShakeID);
}
function onWatchShakeSuccess(newValue) {
alert(newValue.x);
var changes = {},
bound = 2;
if (oldValue.x !== null) {
changes.x = Math.abs(oldValue.x-newValue.x);
changes.y = Math.abs(oldValue.y-newValue.y);
changes.z = Math.abs(oldValue.z-newValue.z);
}
if ((changes.x > bound && changes.y > bound) || (changes.x > bound && changes.z >bound)|| (changes.y > bound && changes.z >bound)) {
alert('检测到手机晃动');
}
oldValue = {
x: newValue.x,
y: newValue.y,
z: newValue.z
}
}
function onWatchShakeError() {
alert('失败');
}
在xcode 5.1调试时候,在模拟器----硬件---摇动手势
没有任何反应。。。。有大神了解是什么问题么?
在安卓机器下可以弹出 x数值
但是在 ios下,只弹出了 alert(watchShakeID);后续的坐标不能弹出来
用真机调试一下模拟器可能没有那个设备 很不错,不错,学习学习。跨平台得顶起 人不错,学习学习 感觉phonegap+html应该是趋势
页:
[1]