最后,我得到了 Ionic 3 的解决方案
private docEvtDevMotion:EventListenerOrEventListenerObject = null;
private lastX:any = null;
constructor() {
let self = this;
this.docEvtDevMotion = (event)=>{
self.motionDetectionHandler(event);
}
this.initMotionDetection();
}
private motionDetectionHandler(event: any) {
if (!this.lastX) {
this.lastX = event.acceleration.x;
this.lastY = event.acceleration.y;
this.lastZ = event.acceleration.z;
return;
}
let deltaX: number, deltaY: number, deltaZ: number;
deltaX = Math.abs(event.acceleration.x - this.lastX);
deltaY = Math.abs(event.acceleration.y - this.lastY);
deltaZ = Math.abs(event.acceleration.z - this.lastZ);
// console.error("Motion ====== " + deltaX + " " + deltaY + " " + deltaZ);
if (deltaX + deltaY + deltaZ > 1) {
window.removeEventListener("devicemotion", this.docEvtDevMotion, false);
}
this.lastX = event.acceleration.x;
this.lastY = event.acceleration.y;
this.lastZ = event.acceleration.z;
}
}
private initMotionDetection(){
window.addEventListener("devicemotion", this.docEvtDevMotion, false);
}