pageY < 30)
return;
if (pageX > this.data.screenWidth - 30)
return;
if (pageY > this.data.screenHeight - 30)
return;
this.setData ({
ballTop: event.touches[0].pageY - 30,
ballLeft: event.touches[0].pageX - 30,
});
},
})
再次运行,一切ok。
手势识别
通过处理滑动操作可以识别各种手势操作,如左右滑动等。思路也很简单,通过绑定touchstart和touchmove事件,然后对坐标信息进行识别计算即可(如current.PageX - last.PageX < 0则认为是向左滑动)
//wxml
<view id="id" class = "ball" bindtap = "handletap" bindtouchstart = "handletouchtart" bindtouchmove="handletouchmove" style = "width : 100%px; height : 40px;">
{{text}}
</view>
//js
Page({
data: {
lastX: 0,
lastY: 0,
text : "没有滑动",
},
handletouchmove: function(event) {
console.log(event)
let currentX = event.touches[0].pageX
let currentY = event.touches[0].pageY
console.log(currentX)
console.log(this.data.lastX)
let text = ""
if ((currentX - this.data.lastX) < 0)
text = "向左滑动"
else if (((currentX - this.data.lastX) > 0))
text = "向右滑动"
//将当前坐标进行保存以进行下一次计算
this.data.lastX = currentX
this.data.lastY = currentY
this.setData({
text : text,
});
},
handletouchtart:function(event) {
console.log(event)
this.data.lastX = event.touches[0].pageX
this.data.lastY = event.touches[0].pageY
},
handletap:function(event) {
console.log(event)
},
})运行程序,当向左滑动时会view会显示"向左滑动", 向右同理。
同时识别左右滑动和上下互动
有时候希望同时识别左右和上下滑动,可以通过比较X轴上的差值和Y轴上的差值,较大的差值为滑动方向。
handletouchmove: function(event) {
console.log(event)
let currentX = event.touches[0].pageX
let currentY = event.touches[0].pageY
let tx = currentX - this.data.lastX
let ty = currentY - this.data.lastY
let text = ""
//左右方向滑动
if (Math.abs(tx) > Math.abs(ty)) {
if (tx < 0)
text = "向左滑动"
else if (tx > 0)
text = "向右滑动"
}
//上下方向滑动
else {
if (ty < 0)
text = "向上滑动"
else if (ty > 0)
text = "向下滑动"
}
//将当前坐标进行保存以进行下一次计算
this.data.lastX = currentX
this.data.lastY = currentY
this.setData({
text : text,
});
},在实际应用中,当某种手势被触发后,在用户没有放开鼠标或手指前,会一直识别为该手势。比如当用户触发左滑手势后,这时再向下滑动,仍要按照左滑手势来处理。
可以定义一个标记来记录第一次识别到的手势,如果已识别出手势,后续的滑动操作就可以忽略,直到用户放开鼠标或手指。放开鼠标或手指操作可以通过绑定handletouchend事件来处理。
Page({
data: {
lastX: 0,
lastY: 0,
text : "没有滑动",
currentGesture: 0,
},
handletouchmove: function(event) {
console.log(event)
if (this.data.currentGesture != 0){
return
}
let currentX = event.touches[0].pageX
let currentY = event.touches[0].pageY
let tx = currentX - this.data.lastX
let ty = currentY - this.data.lastY
let text = ""
//左右方向滑动
if (Math.abs(tx) > Math.abs(ty)) {
if (tx < 0) {
text = "向左滑动"
this.data.currentGesture = 1
}
else if (tx > 0) {
text = "向右滑动"
this.data.currentGesture = 2
}
}
//上下方向滑动
else {
if (ty < 0){
text = "向上滑动"
this.data.currentGesture = 3
}
else if (ty > 0) {
text = "向下滑动"
this.data.currentGesture = 4
}
}
//将当前坐标进行保存以进行下一次计算
this.data.lastX = currentX
this.data.lastY = currentY
this.setData({
text : text,
});
},
handletouchtart:function(event) {
console.log(event)
this.data.lastX = event.touches[0].pageX
this.data.lastY = event.touches[0].pageY
},
handletouchend:function(event) {
console.log(event)
this.data.currentGesture = 0
this.setData({
text : "没有滑动",
});
},
})多点触控
由于多点触控需要真机来测试,而我的appid还在申请中,只能延后讲解了。这里大概提下思路,比如手指张开的操作,可以分别判断两个触摸点的移动方向,比如靠左的触摸点向左滑动,靠右的触摸点向右滑动,即可判定为手指张开操作。
以上就是小程序开发之基础篇滑动操作(10)的详细内容,更多请关注php中文网其它相关文章!
小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用。
关键词:小程序开发之基础篇滑动设置(10)