Revert "chore: remove hooks ooops!"

This reverts commit 8d4d965a85.
This commit is contained in:
陈嘉涵
2020-01-14 17:20:15 +08:00
parent 28da4dce0c
commit 2e6dbca06d
4 changed files with 185 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
import { ref } from 'vue';
const MIN_DISTANCE = 10;
function getDirection(x: number, y: number) {
if (x > y && x > MIN_DISTANCE) {
return 'horizontal';
}
if (y > x && y > MIN_DISTANCE) {
return 'vertical';
}
return '';
}
export function useTouch() {
const startX = ref(0);
const startY = ref(0);
const deltaX = ref(0);
const deltaY = ref(0);
const offsetX = ref(0);
const offsetY = ref(0);
const direction = ref('');
function reset() {
direction.value = '';
deltaX.value = 0;
deltaY.value = 0;
offsetX.value = 0;
offsetY.value = 0;
}
function start(event: TouchEvent) {
reset();
startX.value = event.touches[0].clientX;
startY.value = event.touches[0].clientY;
}
function move(event: TouchEvent) {
const touch = event.touches[0];
deltaX.value = touch.clientX - this.startX;
deltaY.value = touch.clientY - this.startY;
offsetX.value = Math.abs(this.deltaX);
offsetY.value = Math.abs(this.deltaY);
if (!direction.value) {
direction.value = getDirection(offsetX.value, offsetY.value);
}
}
return {
move,
start,
startX,
startY,
deltaX,
deltaY,
offsetX,
offsetY,
direction
};
}