Accelerometer
startAccelerometer
TIP
The API usage is as follows: wx.startAccelerometer(Object object)
TIP
Depending on the performance of the model, the current CPU and memory usage, the interval setting and the actual wx.onAccelerometerChange The execution frequency of the callback function will be slightly different
Functional description: Start listening to acceleration data
Parameters and descriptions: Object object。
Properties Type Default value Required Description interval string normal Yes The execution frequency of the callback function for listening to acceleration data success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) The legal value of object.interval
Value Description game The callback frequency suitable for updating the game is about 20ms/time ui The callback frequency suitable for updating the UI is about 60ms/time normal The normal callback frequency is about 200ms/time Sample code:
wx.startAccelerometer({
interval: 'game'
})stopAccelerometer
TIP
The API usage is as follows: wx.stopAccelerometer(Object object)
Functional description: Stop listening for acceleration data
Parameters and descriptions: Object object。
Properties Type Default value Required Description success Function - No Callback function for successful interface call fail Function - No Callback function for failed interface call complete Function - No Callback function for interface call completion (executed regardless of success or failure) Sample code:
wx.stopAccelerometer()onAccelerometerChange
TIP
The API usage is as follows: wx.onAccelerometerChange(function listener)
Functional description: Listener for device orientation change events. Frequency based on wx.startAccelerometer() interval parameter. You can use wx.stopAccelerometer() Stops monitoring.
Parameters and descriptions: function callback, The callback function of the acceleration data event. The parameters Object.res are as follows:
Properties Type Description x number X axis y number Y axis z number Z axis Sample code:
wx.onAccelerometerChange(function (res) {
console.log(res.x)
console.log(res.y)
console.log(res.z)
})offAccelerometerChange
TIP
The API usage is as follows: wx.offAccelerometerChange(function listener)
Functional description: Remove the listener function of the acceleration data event
Parameters and descriptions: function callback, The listener function passed in by onAccelerometerChange. If this parameter is not passed, all listeners will be removed
Sample code:
const listener = function (res) { console.log(res) }
wx.onAccelerometerChange(listener)
wx.offAccelerometerChange(listener) // Pass the same function object used for listening.