Skip to content

手势识别(useGesture)

useGesture 是一个触摸手势检测 Hook,支持方向滑动检测和长按检测,滑动与长按互斥。

此 API 源码:GitHub

使用方式

vue
<template>
  <view ref="boxRef">滑动或长按此区域</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useGesture } from 'see-u-ui'

const boxRef = ref(null)
const { isSwiping, bindEvents } = useGesture(boxRef, {
  direction: 'horizontal',
  threshold: 10,
  onSwipe: (direction, distance) => {
    console.log(`向${direction}滑动了 ${distance}px`)
  },
  onLongPress: () => {
    console.log('长按触发')
  }
})

onMounted(() => bindEvents())
</script>

API

useGesture(elementRef, options?)

参数类型默认值说明
elementRefRef<HTMLElement | null>目标元素引用
options.direction'horizontal' | 'vertical' | 'both''both'检测方向
options.thresholdnumber10滑动触发阈值(px)
options.longPressDelaynumber350长按触发延迟(ms)
options.onSwipe(direction, distance) => voidundefined滑动回调
options.onSwipeStart() => voidundefined滑动开始回调
options.onSwipeEnd(direction, distance) => voidundefined滑动结束回调
options.onLongPress() => voidundefined长按回调
options.onLongPressEnd() => voidundefined长按结束回调

返回值:

属性/方法类型说明
isSwipingRef<boolean>是否正在滑动
swipeDistanceRef<number>当前滑动距离
isLongPressingRef<boolean>是否正在长按
bindEvents() => void绑定事件监听
unbindEvents() => void解绑事件监听
reset() => void重置状态

类型定义

ts
type SwipeDirection = 'left' | 'right' | 'up' | 'down'
type GestureDirection = 'horizontal' | 'vertical' | 'both'

注意

需要手动调用 bindEvents() 绑定事件,组件卸载时自动解绑。滑动超过阈值会取消待触发的长按。

辽 ICP 备 2025070134 号