const touchHandler = {
startX: 0,
startY: 0,
startTime: 0,
isMoving: false,
longPressTimeout: null,
thresholdDistance: 10,
thresholdTime: 300,
longPressTime: 500,
longPressTriggered: false,
onTouchStart: function(event) {
this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY;
this.startTime = Date.now();
this.isMoving = false;
this.longPressTimeout = setTimeout(() => {
this.handleLongPress(event);
}, this.longPressTime);
},
onTouchMove: function(event) {
this.isMoving = true;
},
onTouchEnd: function(event) {
clearTimeout(this.longPressTimeout);
const endX = event.changedTouches[0].clientX;
const endY = event.changedTouches[0].clientY;
const endTime = Date.now();
const moveX = endX - this.startX;
const moveY = endY - this.startY;
const moveDistance = Math.sqrt(moveX * moveX + moveY * moveY);
const timeDiff = endTime - this.startTime;
if (
timeDiff < this.thresholdTime &&
moveDistance < this.thresholdDistance
) {
this.handleClick(event);
} else if (
this.isMoving &&
!this.longPressTriggered &&
moveDistance > this.thresholdDistance
) {
this.handleSwipe(this.startX, this.startY, endX, endY);
}
this.isMoving = false;
this.longPressTriggered = false;
},
onTouchCancel: function(event) {
clearTimeout(this.longPressTimeout);
this.isMoving = false;
this.longPressTriggered = false;
},
handleClick: function(event) {
console.log("Tap", event);
},
handleLongPress: function(event) {
console.log("Press and hold", event);
this.longPressTriggered = true;
},
handleSwipe: function(startX, startY, endX, endY) {
const deltaX = endX - startX;
const deltaY = endY - startY;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
if (deltaX > 0) {
console.log("Swipe right");
} else {
console.log("Swipe left");
}
} else {
if (deltaY > 0) {
console.log("Swipe down");
} else {
console.log("Swipe up");
}
}
}
};
wx.onTouchStart(touchHandler.onTouchStart.bind(touchHandler));
wx.onTouchMove(touchHandler.onTouchMove.bind(touchHandler));
wx.onTouchEnd(touchHandler.onTouchEnd.bind(touchHandler));
wx.onTouchCancel(touchHandler.onTouchCancel.bind(touchHandler));