ts-universal-events-touch.md 9.4 KB
Newer Older
Z
zengyawen 已提交
1
# 触摸事件<a name="ZH-CN_TOPIC_0000001237475045"></a>
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3 4 5 6 7 8 9 10 11 12
>![](../../public_sys-resources/icon-note.gif) **说明:** 
>从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

## 权限列表<a name="section781125411508"></a>



## 事件<a name="section18817111365714"></a>

<table><thead align="left"><tr><th class="cellrowborder" colspan="2" valign="top" id="mcps1.1.5.1.1"><p>名称</p>
Z
zengyawen 已提交
13
</th>
Z
zengyawen 已提交
14
<th class="cellrowborder" valign="top" id="mcps1.1.5.1.2"><p>是否冒泡</p>
Z
zengyawen 已提交
15
</th>
Z
zengyawen 已提交
16
<th class="cellrowborder" valign="top" id="mcps1.1.5.1.3"><p>功能描述</p>
Z
zengyawen 已提交
17 18 19
</th>
</tr>
</thead>
Z
zengyawen 已提交
20
<tbody><tr><td class="cellrowborder" colspan="2" valign="top" headers="mcps1.1.5.1.1 "><p>onTouch(callback: (event?: TouchEvent) =&gt; void)</p>
Z
zengyawen 已提交
21
</td>
Z
zengyawen 已提交
22
<td class="cellrowborder" valign="top" headers="mcps1.1.5.1.2 "><p></p>
Z
zengyawen 已提交
23
</td>
Z
zengyawen 已提交
24
<td class="cellrowborder" valign="top" headers="mcps1.1.5.1.3 "><p>触摸动作触发该方法调用,event参数见<a href="#section454663343911">TouchEvent</a>介绍。</p>
Z
zengyawen 已提交
25 26 27 28 29 30 31 32 33
</td>
</tr>
</tbody>
</table>

## TouchEvent对象说明<a name="section454663343911"></a>

-   属性

Z
zengyawen 已提交
34
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="22.509999999999998%" id="mcps1.1.4.1.1"><p>属性名称</p>
Z
zengyawen 已提交
35
    </th>
Z
zengyawen 已提交
36
    <th class="cellrowborder" valign="top" width="22.7%" id="mcps1.1.4.1.2"><p>类型</p>
Z
zengyawen 已提交
37
    </th>
Z
zengyawen 已提交
38
    <th class="cellrowborder" valign="top" width="54.790000000000006%" id="mcps1.1.4.1.3"><p>描述</p>
Z
zengyawen 已提交
39 40 41
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
42 43 44 45 46 47 48 49
    <tbody><tr><td class="cellrowborder" valign="top" width="22.509999999999998%" headers="mcps1.1.4.1.1 "><p>type</p>
    </td>
    <td class="cellrowborder" valign="top" width="22.7%" headers="mcps1.1.4.1.2 "><p><a href="#li16235181314454">TouchType</a></p>
    </td>
    <td class="cellrowborder" valign="top" width="54.790000000000006%" headers="mcps1.1.4.1.3 "><p>触摸事件的类型。</p>
    </td>
    </tr>
    <tr><td class="cellrowborder" valign="top" width="22.509999999999998%" headers="mcps1.1.4.1.1 "><p>touches</p>
Z
zengyawen 已提交
50
    </td>
Z
zengyawen 已提交
51
    <td class="cellrowborder" valign="top" width="22.7%" headers="mcps1.1.4.1.2 "><p>Array&lt;<a href="#li1783344810442">TouchObject</a>&gt;</p>
Z
zengyawen 已提交
52
    </td>
Z
zengyawen 已提交
53
    <td class="cellrowborder" valign="top" width="54.790000000000006%" headers="mcps1.1.4.1.3 "><p>全部手指信息。</p>
Z
zengyawen 已提交
54 55
    </td>
    </tr>
Z
zengyawen 已提交
56
    <tr><td class="cellrowborder" valign="top" width="22.509999999999998%" headers="mcps1.1.4.1.1 "><p>changedTouches</p>
Z
zengyawen 已提交
57
    </td>
Z
zengyawen 已提交
58
    <td class="cellrowborder" valign="top" width="22.7%" headers="mcps1.1.4.1.2 "><p>Array&lt;<a href="#li1783344810442">TouchObject</a>&gt;</p>
Z
zengyawen 已提交
59
    </td>
Z
zengyawen 已提交
60
    <td class="cellrowborder" valign="top" width="54.790000000000006%" headers="mcps1.1.4.1.3 "><p>当前发生变化的手指信息。</p>
Z
zengyawen 已提交
61 62
    </td>
    </tr>
Z
zengyawen 已提交
63
    <tr><td class="cellrowborder" valign="top" width="22.509999999999998%" headers="mcps1.1.4.1.1 "><p>timestamp</p>
Z
zengyawen 已提交
64
    </td>
Z
zengyawen 已提交
65
    <td class="cellrowborder" valign="top" width="22.7%" headers="mcps1.1.4.1.2 "><p>number</p>
Z
zengyawen 已提交
66
    </td>
Z
zengyawen 已提交
67
    <td class="cellrowborder" valign="top" width="54.790000000000006%" headers="mcps1.1.4.1.3 "><p>事件时间戳。</p>
Z
zengyawen 已提交
68 69
    </td>
    </tr>
Z
zengyawen 已提交
70
    <tr><td class="cellrowborder" valign="top" width="22.509999999999998%" headers="mcps1.1.4.1.1 "><p>target<sup><span>8+</span></sup></p>
Z
zengyawen 已提交
71
    </td>
Z
zengyawen 已提交
72
    <td class="cellrowborder" valign="top" width="22.7%" headers="mcps1.1.4.1.2 "><p><a href="ts-universal-events-click.md#li552912253714">EventTarget</a></p>
Z
zengyawen 已提交
73
    </td>
Z
zengyawen 已提交
74
    <td class="cellrowborder" valign="top" width="54.790000000000006%" headers="mcps1.1.4.1.3 "><p>被触摸元素对象。</p>
Z
zengyawen 已提交
75 76 77 78 79 80 81 82
    </td>
    </tr>
    </tbody>
    </table>


-   接口

Z
zengyawen 已提交
83
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="44.78%" id="mcps1.1.3.1.1"><p>接口名称</p>
Z
zengyawen 已提交
84
    </th>
Z
zengyawen 已提交
85
    <th class="cellrowborder" valign="top" width="55.22%" id="mcps1.1.3.1.2"><p>功能描述</p>
Z
zengyawen 已提交
86 87 88
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
89
    <tbody><tr><td class="cellrowborder" valign="top" width="44.78%" headers="mcps1.1.3.1.1 "><p>stopPropagation():void</p>
Z
zengyawen 已提交
90
    </td>
Z
zengyawen 已提交
91
    <td class="cellrowborder" valign="top" width="55.22%" headers="mcps1.1.3.1.2 "><p>阻塞事件冒泡。</p>
Z
zengyawen 已提交
92 93 94 95 96 97 98 99
    </td>
    </tr>
    </tbody>
    </table>


-   <a name="li1783344810442"></a>TouchObject对象说明

Z
zengyawen 已提交
100
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="18.55%" id="mcps1.1.4.1.1"><p>属性名称</p>
Z
zengyawen 已提交
101
    </th>
Z
zengyawen 已提交
102
    <th class="cellrowborder" valign="top" width="25.45%" id="mcps1.1.4.1.2"><p>类型</p>
Z
zengyawen 已提交
103
    </th>
Z
zengyawen 已提交
104
    <th class="cellrowborder" valign="top" width="56.00000000000001%" id="mcps1.1.4.1.3"><p>描述</p>
Z
zengyawen 已提交
105 106 107
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
108
    <tbody><tr><td class="cellrowborder" valign="top" width="18.55%" headers="mcps1.1.4.1.1 "><p>type</p>
Z
zengyawen 已提交
109
    </td>
Z
zengyawen 已提交
110
    <td class="cellrowborder" valign="top" width="25.45%" headers="mcps1.1.4.1.2 "><p><a href="#li16235181314454">TouchType</a></p>
Z
zengyawen 已提交
111
    </td>
Z
zengyawen 已提交
112
    <td class="cellrowborder" valign="top" width="56.00000000000001%" headers="mcps1.1.4.1.3 "><p>触摸事件的类型。</p>
Z
zengyawen 已提交
113 114
    </td>
    </tr>
Z
zengyawen 已提交
115
    <tr><td class="cellrowborder" valign="top" width="18.55%" headers="mcps1.1.4.1.1 "><p>id</p>
Z
zengyawen 已提交
116
    </td>
Z
zengyawen 已提交
117
    <td class="cellrowborder" valign="top" width="25.45%" headers="mcps1.1.4.1.2 "><p>number</p>
Z
zengyawen 已提交
118
    </td>
Z
zengyawen 已提交
119
    <td class="cellrowborder" valign="top" width="56.00000000000001%" headers="mcps1.1.4.1.3 "><p>手指唯一标识符。</p>
Z
zengyawen 已提交
120 121
    </td>
    </tr>
Z
zengyawen 已提交
122
    <tr><td class="cellrowborder" valign="top" width="18.55%" headers="mcps1.1.4.1.1 "><p>screenX</p>
Z
zengyawen 已提交
123
    </td>
Z
zengyawen 已提交
124
    <td class="cellrowborder" valign="top" width="25.45%" headers="mcps1.1.4.1.2 "><p>number</p>
Z
zengyawen 已提交
125
    </td>
Z
zengyawen 已提交
126
    <td class="cellrowborder" valign="top" width="56.00000000000001%" headers="mcps1.1.4.1.3 "><p>触摸点相对于设备屏幕左边沿的X坐标。</p>
Z
zengyawen 已提交
127 128
    </td>
    </tr>
Z
zengyawen 已提交
129
    <tr><td class="cellrowborder" valign="top" width="18.55%" headers="mcps1.1.4.1.1 "><p>screenY</p>
Z
zengyawen 已提交
130
    </td>
Z
zengyawen 已提交
131
    <td class="cellrowborder" valign="top" width="25.45%" headers="mcps1.1.4.1.2 "><p>number</p>
Z
zengyawen 已提交
132
    </td>
Z
zengyawen 已提交
133
    <td class="cellrowborder" valign="top" width="56.00000000000001%" headers="mcps1.1.4.1.3 "><p>触摸点相对于设备屏幕上边沿的Y坐标。</p>
Z
zengyawen 已提交
134 135
    </td>
    </tr>
Z
zengyawen 已提交
136
    <tr><td class="cellrowborder" valign="top" width="18.55%" headers="mcps1.1.4.1.1 "><p>x</p>
Z
zengyawen 已提交
137
    </td>
Z
zengyawen 已提交
138
    <td class="cellrowborder" valign="top" width="25.45%" headers="mcps1.1.4.1.2 "><p>number</p>
Z
zengyawen 已提交
139
    </td>
Z
zengyawen 已提交
140
    <td class="cellrowborder" valign="top" width="56.00000000000001%" headers="mcps1.1.4.1.3 "><p>触摸点相对于被触摸元素左边沿的X坐标。</p>
Z
zengyawen 已提交
141 142
    </td>
    </tr>
Z
zengyawen 已提交
143
    <tr><td class="cellrowborder" valign="top" width="18.55%" headers="mcps1.1.4.1.1 "><p>y</p>
Z
zengyawen 已提交
144
    </td>
Z
zengyawen 已提交
145
    <td class="cellrowborder" valign="top" width="25.45%" headers="mcps1.1.4.1.2 "><p>number</p>
Z
zengyawen 已提交
146
    </td>
Z
zengyawen 已提交
147
    <td class="cellrowborder" valign="top" width="56.00000000000001%" headers="mcps1.1.4.1.3 "><p>触摸点相对于被触摸元素上边沿的Y坐标。</p>
Z
zengyawen 已提交
148 149 150 151 152 153 154 155
    </td>
    </tr>
    </tbody>
    </table>


-   <a name="li16235181314454"></a>TouchType枚举说明

Z
zengyawen 已提交
156
    <table><thead align="left"><tr><th class="cellrowborder" valign="top" width="25.2%" id="mcps1.1.3.1.1"><p>名称</p>
Z
zengyawen 已提交
157
    </th>
Z
zengyawen 已提交
158
    <th class="cellrowborder" valign="top" width="74.8%" id="mcps1.1.3.1.2"><p>描述</p>
Z
zengyawen 已提交
159 160 161
    </th>
    </tr>
    </thead>
Z
zengyawen 已提交
162
    <tbody><tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Down</p>
Z
zengyawen 已提交
163
    </td>
Z
zengyawen 已提交
164
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>手指按下时触发。</p>
Z
zengyawen 已提交
165 166
    </td>
    </tr>
Z
zengyawen 已提交
167
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Up</p>
Z
zengyawen 已提交
168
    </td>
Z
zengyawen 已提交
169
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>手指抬起时触发。</p>
Z
zengyawen 已提交
170 171
    </td>
    </tr>
Z
zengyawen 已提交
172
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Move</p>
Z
zengyawen 已提交
173
    </td>
Z
zengyawen 已提交
174
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>手指按压态在屏幕上移动时触发。</p>
Z
zengyawen 已提交
175 176
    </td>
    </tr>
Z
zengyawen 已提交
177
    <tr><td class="cellrowborder" valign="top" width="25.2%" headers="mcps1.1.3.1.1 "><p>Cancel</p>
Z
zengyawen 已提交
178
    </td>
Z
zengyawen 已提交
179
    <td class="cellrowborder" valign="top" width="74.8%" headers="mcps1.1.3.1.2 "><p>触摸事件取消时触发。</p>
Z
zengyawen 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    </td>
    </tr>
    </tbody>
    </table>


## 示例<a name="section13817013123017"></a>

```
@Entry
@Component
struct TouchExample {
  @State text: string = ''
  @State eventType: string = ''

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Z
zengyawen 已提交
197
      Button('Touch').backgroundColor(0x2788D9).height(40).width(80)
Z
zengyawen 已提交
198 199 200 201 202 203 204 205 206 207
        .onTouch((event: TouchEvent) => {
          if (event.type === TouchType.Down) {
            this.eventType = 'Down'
          }
          if (event.type === TouchType.Up) {
            this.eventType = 'Up'
          }
          if (event.type === TouchType.Move) {
            this.eventType = 'Move'
          }
Z
zengyawen 已提交
208 209 210 211
          console.info(this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
          + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\ncomponent globalPos:('
          + event.target.area.globalPos.x + ',' + event.target.area.globalPos.y + ')\nwidth:'
          + event.target.area.width + '\nheight:' + event.target.area.height)
Z
zengyawen 已提交
212 213 214 215 216 217 218
        })
      Text(this.text)
    }.height(200).width(350).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
219
![](figures/zh-cn_image_0000001192915178.gif)
Z
zengyawen 已提交
220