hy-app 0.7.1 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,237 +0,0 @@
1
- # useTouch 触摸事件组合式 API
2
-
3
- ## 功能介绍
4
-
5
- `useTouch` 是一个基于 Vue 3 Composition API 的触摸事件处理钩子,用于跟踪和分析用户的触摸操作,包括触摸方向、位移距离等信息。
6
-
7
- - 支持跟踪触摸的水平和垂直方向
8
- - 提供实时的位移数据
9
- - 支持响应式状态管理
10
- - 轻量简洁,易于集成
11
-
12
- ## 引入方式
13
-
14
- ```typescript
15
- import { useTouch } from '@/package/libs/composables/useTouch'
16
- ```
17
-
18
- ## API 文档
19
-
20
- ### useTouch()
21
-
22
- 创建并返回触摸事件处理实例,包含触摸事件处理函数和触摸状态。
23
-
24
- **返回值**:包含以下属性的对象:
25
-
26
- | 返回值 | 类型 | 说明 |
27
- |--------|------|------|
28
- | touchStart | (event: any) => void | 触摸开始事件处理函数 |
29
- | touchMove | (event: any) => void | 触摸移动事件处理函数 |
30
- | direction | Ref<string> | 触摸方向,值为 "horizontal"(水平)、"vertical"(垂直)或 ""(未确定) |
31
- | deltaX | Ref<number> | 水平方向位移,正数表示向右,负数表示向左 |
32
- | deltaY | Ref<number> | 垂直方向位移,正数表示向下,负数表示向上 |
33
- | offsetX | Ref<number> | 水平方向位移的绝对值 |
34
- | offsetY | Ref<number> | 垂直方向位移的绝对值 |
35
- | startX | Ref<number> | 触摸开始时的 X 坐标 |
36
- | startY | Ref<number> | 触摸开始时的 Y 坐标 |
37
-
38
- ## 使用示例
39
-
40
- ### 基础用法
41
-
42
- ```vue
43
- <template>
44
- <div
45
- class="touch-area"
46
- @touchstart="touchStart"
47
- @touchmove="touchMove"
48
- >
49
- <div class="info">
50
- <p>触摸方向: {{ direction }}</p>
51
- <p>水平位移: {{ deltaX }}px</p>
52
- <p>垂直位移: {{ deltaY }}px</p>
53
- <p>水平偏移: {{ offsetX }}px</p>
54
- <p>垂直偏移: {{ offsetY }}px</p>
55
- </div>
56
- </div>
57
- </template>
58
-
59
- <script setup lang="ts">
60
- import { useTouch } from '@/package/libs/composables/useTouch'
61
-
62
- const {
63
- touchStart,
64
- touchMove,
65
- direction,
66
- deltaX,
67
- deltaY,
68
- offsetX,
69
- offsetY
70
- } = useTouch()
71
- </script>
72
-
73
- <style scoped>
74
- .touch-area {
75
- width: 100%;
76
- height: 300px;
77
- background-color: #f5f5f5;
78
- display: flex;
79
- align-items: center;
80
- justify-content: center;
81
- border-radius: 8px;
82
- touch-action: none;
83
- }
84
-
85
- .info {
86
- text-align: center;
87
- font-size: 14px;
88
- color: #333;
89
- }
90
-
91
- .info p {
92
- margin: 8px 0;
93
- }
94
- </style>
95
- ```
96
-
97
- ### 高级用法 - 实现滑动手势
98
-
99
- ```vue
100
- <template>
101
- <div
102
- class="swipe-container"
103
- @touchstart="touchStart"
104
- @touchmove="touchMove"
105
- @touchend="touchEnd"
106
- >
107
- <div
108
- class="swipe-content"
109
- :style="{
110
- transform: `translateX(${swipeOffset}px)`
111
- }"
112
- >
113
- 滑动内容区域
114
- </div>
115
- </div>
116
- </template>
117
-
118
- <script setup lang="ts">
119
- import { ref, computed } from 'vue'
120
- import { useTouch } from '@/package/libs/composables/useTouch'
121
-
122
- const {
123
- touchStart,
124
- touchMove,
125
- direction,
126
- deltaX
127
- } = useTouch()
128
-
129
- const swipeOffset = ref(0)
130
- const maxSwipeOffset = 100
131
-
132
- const touchEnd = () => {
133
- if (direction.value === 'horizontal') {
134
- if (deltaX.value > 50) {
135
- // 向右滑动超过阈值
136
- swipeOffset.value = maxSwipeOffset
137
- } else if (deltaX.value < -50) {
138
- // 向左滑动超过阈值
139
- swipeOffset.value = -maxSwipeOffset
140
- } else {
141
- // 复位
142
- swipeOffset.value = 0
143
- }
144
- }
145
- }
146
- </script>
147
-
148
- <style scoped>
149
- .swipe-container {
150
- width: 100%;
151
- height: 100px;
152
- overflow: hidden;
153
- position: relative;
154
- background-color: #f5f5f5;
155
- border-radius: 8px;
156
- }
157
-
158
- .swipe-content {
159
- width: 100%;
160
- height: 100%;
161
- background-color: #409eff;
162
- color: white;
163
- display: flex;
164
- align-items: center;
165
- justify-content: center;
166
- transition: transform 0.3s ease;
167
- }
168
- </style>
169
- ```
170
-
171
- ## 技术实现
172
-
173
- ### 核心原理
174
-
175
- 1. **响应式状态管理**:使用 Vue 3 的 `ref` 创建响应式变量,跟踪触摸状态
176
- 2. **触摸事件处理**:通过 `touchStart` 和 `touchMove` 函数处理触摸事件
177
- 3. **方向判断**:根据水平和垂直位移的绝对值大小判断触摸方向
178
- 4. **位移计算**:实时计算触摸点的位移距离和偏移量
179
-
180
- ### 关键函数
181
-
182
- #### touchStart(event: any)
183
-
184
- 触摸开始时的事件处理函数,初始化所有状态变量。
185
-
186
- - **参数**:`event` - 触摸事件对象
187
- - **功能**:
188
- - 获取第一个触摸点
189
- - 重置方向和位移状态
190
- - 记录触摸开始的坐标位置
191
-
192
- #### touchMove(event: any)
193
-
194
- 触摸移动时的事件处理函数,计算位移和方向。
195
-
196
- - **参数**:`event` - 触摸事件对象
197
- - **功能**:
198
- - 获取当前触摸点
199
- - 计算水平和垂直方向的位移
200
- - 计算位移的绝对值
201
- - 根据位移绝对值判断触摸方向
202
-
203
- ## 返回值详解
204
-
205
- | 返回值 | 类型 | 说明 |
206
- |--------|------|------|
207
- | touchStart | Function | 触摸开始事件处理函数,需绑定到元素的 @touchstart 事件 |
208
- | touchMove | Function | 触摸移动事件处理函数,需绑定到元素的 @touchmove 事件 |
209
- | direction | Ref<string> | 触摸方向,值为 "horizontal"(水平)、"vertical"(垂直)或 ""(未确定) |
210
- | deltaX | Ref<number> | 水平方向位移,正数表示向右,负数表示向左 |
211
- | deltaY | Ref<number> | 垂直方向位移,正数表示向下,负数表示向上 |
212
- | offsetX | Ref<number> | 水平方向位移的绝对值 |
213
- | offsetY | Ref<number> | 垂直方向位移的绝对值 |
214
- | startX | Ref<number> | 触摸开始时的 X 坐标 |
215
- | startY | Ref<number> | 触摸开始时的 Y 坐标 |
216
-
217
- ## 注意事项
218
-
219
- 1. **事件绑定**:需要在目标元素上同时绑定 `@touchstart` 和 `@touchmove` 事件
220
- 2. **触摸设备**:此钩子仅在支持触摸事件的设备上有效
221
- 3. **性能优化**:对于频繁的触摸移动事件,可以考虑使用 `passive` 事件监听器或节流处理
222
- 4. **浏览器兼容性**:不同浏览器对触摸事件的实现可能存在差异,建议在主要目标设备上进行测试
223
- 5. **事件对象类型**:当前实现中 `event` 参数类型为 `any`,在 TypeScript 项目中可以根据需要定义更具体的类型
224
-
225
- ## 应用场景
226
-
227
- 1. **滑动手势识别**:如左右滑动切换内容、上下滑动刷新等
228
- 2. **拖拽功能**:实现元素的拖拽移动
229
- 3. **手势密码**:辅助实现手势密码功能
230
- 4. **游戏控制**:用于游戏中的触摸控制
231
- 5. **交互式组件**:如滑块、滑动菜单等组件的实现
232
-
233
- ## 总结
234
-
235
- `useTouch` 是一个轻量级的触摸事件处理钩子,提供了简单而强大的触摸状态跟踪功能。通过使用它,开发者可以轻松实现各种基于触摸的交互效果,提升应用的用户体验。
236
-
237
- 该钩子返回的响应式状态和事件处理函数可以与 Vue 3 的组合式 API 无缝集成,适用于各种需要触摸交互的场景。