From 3057f6366b8a66e4ac1e2fca1dbebcd65a50f988 Mon Sep 17 00:00:00 2001 From: icssoa <615206459@qq.com> Date: Thu, 13 Nov 2025 22:35:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20useTouch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/cool-ui/hooks/index.ts | 1 + uni_modules/cool-ui/hooks/touch.ts | 42 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 uni_modules/cool-ui/hooks/touch.ts diff --git a/uni_modules/cool-ui/hooks/index.ts b/uni_modules/cool-ui/hooks/index.ts index 17b1a58..598f801 100644 --- a/uni_modules/cool-ui/hooks/index.ts +++ b/uni_modules/cool-ui/hooks/index.ts @@ -2,4 +2,5 @@ export * from "./component"; export * from "./form"; export * from "./page"; export * from "./size"; +export * from "./touch"; export * from "./ui"; diff --git a/uni_modules/cool-ui/hooks/touch.ts b/uni_modules/cool-ui/hooks/touch.ts new file mode 100644 index 0000000..b54e1f9 --- /dev/null +++ b/uni_modules/cool-ui/hooks/touch.ts @@ -0,0 +1,42 @@ +export class Touch { + // 触摸起始Y坐标 + startY = 0; + // 触摸起始X坐标 + startX = 0; + // 横向滑动参数 + horizontal = 0; + + start(e: UniTouchEvent) { + this.startX = e.touches[0].clientX; + this.startY = e.touches[0].clientY; + this.horizontal = 0; + } + + move(e: UniTouchEvent) { + const x = this.startX - e.touches[0].clientX; + + if (this.horizontal == 0) { + // 只在horizontal=0时判断一次 + const y = this.startY - e.touches[0].clientY; + + if (Math.abs(x) > Math.abs(y)) { + // 如果x轴移动距离大于y轴移动距离则表明是横向移动手势 + this.horizontal = 1; + } + + if (this.horizontal == 1) { + // 如果是横向移动手势,则阻止默认行为(防止页面滚动) + e.preventDefault(); + } + } + } + + end() { + this.startY = 0; + this.horizontal = 0; + } +} + +export function useTouch(): Touch { + return new Touch(); +}