微信小程序罗盘、性能监控、加速计、剪贴板实例
由于这些实例需要使用手机的硬件,所以无法用电脑录屏演示。
wxml
<view>
<button bindtap="startCompass">罗盘监控开始</button>
<button bindtap="endCompass">罗盘监控结束</button>
<button bindtap="memoryWaring">性能监控</button>
<button bindtap="accelerometer">加速计</button>
<button bindtap="setClipboard">剪贴板</button>
</view>
js
下面是js中的绑定事件
startCompass:function(){
wx.startCompass({
success: (res) => {
wx.onCompassChange((result) => {
console.log(result);
})
},
fail: (res) => {},
complete: (res) => {},
})
},
endCompass:function(){
wx.stopCompass({
success: (res) => {},
})
},
memoryWaring:function(){
wx.onMemoryWarning((result) => {
console.log(result);
})
},
accelerometer:function(){
wx.startAccelerometer({
interval: "game",
})
//game:20ms一次,ui:60ms一次,normal:200ms一次
wx.onAccelerometerChange((result) => {
console.log("x:"+result.x);
console.log("y:"+result.y);
console.log("z:"+result.z);
})
},
setClipboard:function(){
wx.setClipboardData({
data: ‘hello world‘,
success(res){
wx.getClipboardData({
success: (option) => {
console.log(option.data);
},
})
}
})
},