Accelerometer
API Reference: Device.Accelerometer
Accelerometer allows capturing accelerometer (motion) events on the device.
TypeScript
1
import PageSampleDesign from 'generated/pages/pageSample';
2
import FlexLayout from '@smartface/native/ui/flexlayout';
3
import Application from '@smartface/native/application';
4
import Accelerometer from '@smartface/native/device/accelerometer';
5
6
7
//You should create new Page from UI-Editor and extend with it.
8
export default class Sample extends PageSampleDesign {
9
10
constructor() {
11
super();
12
// Overrides super.onShow method
13
this.onShow = onShow.bind(this, this.onShow.bind(this));
14
// Overrides super.onLoad method
15
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
16
17
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
18
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
19
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
20
}
21
}
22
23
/**
24
* @event onShow
25
* This event is called when a page appears on the screen (everytime).
26
* @param {function} superOnShow super onShow function
27
* @param {Object} parameters passed from Router.go function
28
*/
29
function onShow(superOnShow: () => void) {
30
const { headerBar } = this;
31
superOnShow();
32
Application.statusBar.visible = false;
33
headerBar.visible = false;
34
}
35
36
/**
37
* @event onLoad
38
* This event is called once when page is created.
39
* @param {function} superOnLoad super onLoad function
40
*/
41
function onLoad(superOnLoad: () => void) {
42
superOnLoad();
43
44
Accelerometer.start();
45
Accelerometer.onAccelerate = (event: { x: number, y: number, z: number }): void => {
46
console.log("x: " + event.x + " y : " + event.y + " z : " + event.z);
47
48
if (event.z > 9) {
49
Accelerometer.stop();
50
}
51
52
};
53
}
54
Copied!
Last modified 2mo ago
Copy link