mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
New do
This commit is contained in:
24
ionic/components/app/test/native/pages/device-orientation.ts
Normal file
24
ionic/components/app/test/native/pages/device-orientation.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import {IonicView, DeviceOrientation} from 'ionic/ionic';
|
||||
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Device Motion</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class DeviceMotionPage {
|
||||
constructor() {
|
||||
let device = DeviceOrientation.watchHeading().source.subscribe((resp) => {
|
||||
console.log('Device orientation', resp);
|
||||
}, (err) => {
|
||||
console.log('Device err', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ export class DeviceMotion {
|
||||
}, function (err) {
|
||||
reject(err);
|
||||
});
|
||||
else {
|
||||
} else {
|
||||
this.pluginWarn();
|
||||
reject('The Device does not support device motion events.');
|
||||
return;
|
||||
|
||||
84
ionic/native/device-orientation/device-orientation.ts
Normal file
84
ionic/native/device-orientation/device-orientation.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import * as Rx from 'rx';
|
||||
|
||||
import * as util from 'ionic/util';
|
||||
import {NativePlugin} from '../plugin';
|
||||
import {Platform} from '../../platform/platform';
|
||||
|
||||
@NativePlugin({
|
||||
name: 'Device Orientation',
|
||||
platforms: {
|
||||
cordova: 'cordova-plugin-device-orientation'
|
||||
}
|
||||
})
|
||||
export class DeviceOrientation {
|
||||
static _wrap(result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static getCurrentAcceleration() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(window.DeviceOrientationEvent) {
|
||||
var fnCb = function fnCb(eventData) {
|
||||
resolve(eventData);
|
||||
window.removeEventListener('deviceorientation', fnCb);
|
||||
}
|
||||
window.addEventListener('deviceorientation', fnCb);
|
||||
} else if(navigator.compass) {
|
||||
navigator.compass.getCurrentHeading(function (result) {
|
||||
resolve(DeviceOrientation._wrap(result));
|
||||
}, function (err) {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
this.pluginWarn();
|
||||
reject('The Device does not support device orientation events.');
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static watchHeading(options) {
|
||||
if(window.DeviceOrientationEvent) {
|
||||
let watchID;
|
||||
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
var fnCb = function fnCb(eventData) {
|
||||
console.log(eventData);
|
||||
observer.onNext(eventData);
|
||||
};
|
||||
|
||||
window.addEventListener('deviceorientation', fnCb);
|
||||
|
||||
});
|
||||
|
||||
return {
|
||||
source: source,
|
||||
watchID: watchID,
|
||||
clear: () => {
|
||||
window.removeEventListener('deviceorientation', cbFn);
|
||||
}
|
||||
}
|
||||
} else if(navigator.accelerometer) {
|
||||
let watchID;
|
||||
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
watchID = navigator.compass.watchHeading(function (result) {
|
||||
observer.onNext(result);
|
||||
}, function (err) {
|
||||
observer.onError(err, observer);
|
||||
}, options);
|
||||
|
||||
});
|
||||
|
||||
return {
|
||||
source: source,
|
||||
watchID: watchID,
|
||||
clear: () => {
|
||||
navigator.compass.clearWatch(watchID);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user