useSwipeBack.uts
4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// composables/useSwipeBack.uts
// 左滑返回检测 Composable - 用于 iOS 侧滑返回手势检测
import { ref, type Ref } from 'vue'
import type { UniTouchEvent, UniTouch } from '@dcloudio/uni-app'
/**
* 左滑返回配置选项
*/
export type SwipeBackOptions = {
/** 滑动距离阈值(像素),默认 80 */
threshold?: number
/** 滑动时间阈值(毫秒),默认 300 */
timeThreshold?: number
/** 角度阈值(度),默认 30,避免误触上下滑动 */
angleThreshold?: number
/** 垂直滑动容差(像素),超过此值取消检测,默认 20 */
verticalTolerance?: number
/** 是否仅在屏幕左侧边缘触发,默认 false */
edgeOnly?: boolean
/** 左侧边缘宽度(像素),仅当 edgeOnly 为 true 时有效,默认 50 */
edgeWidth?: number
}
/**
* 左滑返回检测结果
*/
export type SwipeBackResult = {
/** 触摸事件处理函数 */
onTouchStart: (e: UniTouchEvent) => void
/** 触摸移动事件处理函数 */
onTouchMove: (e: UniTouchEvent) => void
/** 触摸结束事件处理函数 */
onTouchEnd: (e: UniTouchEvent) => void
/** 是否正在滑动 */
isSwiping: Ref<boolean>
/** 重置滑动状态 */
reset: () => void
}
/**
* 左滑返回检测 Hook
*
* @param onSwipeBack - 检测到左滑返回时的回调函数
* @param options - 配置选项
* @returns 触摸事件处理函数和状态
*
* @example
* ```typescript
* const { onTouchStart, onTouchMove, onTouchEnd } = useSwipeBack(() => {
* // 显示确认弹窗或执行返回逻辑
* showConfirmModal.value = true
* })
* ```
*
* @example
* ```vue
* <template>
* <view
* @touchstart="onTouchStart"
* @touchmove="onTouchMove"
* @touchend="onTouchEnd"
* >
* <!-- 页面内容 -->
* </view>
* </template>
* ```
*/
export function useSwipeBack(
onSwipeBack: () => void,
options: SwipeBackOptions = {}
): SwipeBackResult {
const {
threshold = 80,
timeThreshold = 300,
angleThreshold = 30,
verticalTolerance = 20,
edgeOnly = false,
edgeWidth = 50
} = options
// 内部状态
const startX = ref<number>(0)
const startY = ref<number>(0)
const startTime = ref<number>(0)
const isSwiping = ref<boolean>(false)
/**
* 触摸开始
*/
function onTouchStart(e: UniTouchEvent): void {
const touches = e.touches
if (touches.length == 0) {
return
}
const firstTouch = touches[0] as UniTouch
// 如果启用边缘检测,检查是否在左侧边缘区域
if (edgeOnly && firstTouch.clientX > edgeWidth) {
return
}
startX.value = firstTouch.clientX
startY.value = firstTouch.clientY
startTime.value = Date.now()
isSwiping.value = true
}
/**
* 触摸移动
*/
function onTouchMove(e: UniTouchEvent): void {
if (!isSwiping.value) {
return
}
const touches = e.touches
if (touches.length == 0) {
return
}
const firstTouch = touches[0] as UniTouch
const deltaY = firstTouch.clientY - startY.value
// 如果垂直滑动距离过大,取消左滑检测(避免与滚动冲突)
const absDeltaY = deltaY >= 0 ? deltaY : -deltaY
if (absDeltaY > verticalTolerance) {
isSwiping.value = false
}
}
/**
* 触摸结束
*/
function onTouchEnd(e: UniTouchEvent): void {
if (!isSwiping.value) {
return
}
isSwiping.value = false
const touches = e.changedTouches
if (touches.length == 0) {
return
}
const endTouch = touches[0] as UniTouch
const deltaX = endTouch.clientX - startX.value
const deltaY = endTouch.clientY - startY.value
const deltaTime = Date.now() - startTime.value
// 计算滑动距离
const absDeltaX = deltaX >= 0 ? deltaX : -deltaX
const absDeltaY = deltaY >= 0 ? deltaY : -deltaY
// 判断是否为有效的右滑手势(从左向右滑,即返回手势)
// 条件:
// 1. 向右滑动(deltaX > 0)
// 2. 滑动距离超过阈值
// 3. 滑动时间在合理范围内
// 4. 滑动角度符合要求(主要是水平滑动)
if (
deltaX > 0 &&
absDeltaX > threshold &&
deltaTime < timeThreshold
) {
// 计算滑动角度,确保主要是水平滑动
const angle = Math.atan2(absDeltaY, absDeltaX) * 180 / Math.PI
if (angle < angleThreshold) {
console.log('[useSwipeBack] 检测到右滑返回手势')
onSwipeBack()
}
}
}
/**
* 重置滑动状态
*/
function reset(): void {
isSwiping.value = false
startX.value = 0
startY.value = 0
startTime.value = 0
}
return {
onTouchStart,
onTouchMove,
onTouchEnd,
isSwiping,
reset
}
}