ts-universal-events-touch.md 3.4 KB
Newer Older
Z
zengyawen 已提交
1
# 触摸事件
Z
zengyawen 已提交
2

H
HelloCrease 已提交
3
> **说明:**
H
HelloCrease 已提交
4
>
Z
zengyawen 已提交
5
> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8 9

## 事件

T
tianyu 已提交
10 11 12
| 名称                                                         | 是否冒泡 | 功能描述                                                     |
| ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
| onTouch(event: (event?: TouchEvent) => void) | 是       | 触摸动作触发该方法调用,event参数见[TouchEvent](#touchevent对象说明)介绍。 |
Z
zengyawen 已提交
13 14


H
HelloCrease 已提交
15
## TouchEvent对象说明
Z
zengyawen 已提交
16

L
luoying_ace_admin 已提交
17 18 19 20 21 22 23 24 25
  | 名称                | 类型                                       | 描述           |
  | ------------------- | ---------------------------------------- | ------------ |
  | type                | [TouchType](ts-appendix-enums.md#touchtype)      | 触摸事件的类型。     |
  | touches             | Array<[TouchObject](#touchobject对象说明)> | 全部手指信息。      |
  | changedTouches      | Array<[TouchObject](#touchobject对象说明)> | 当前发生变化的手指信息。 |
  | stopPropagation      | () => void | 阻塞事件冒泡。 |
  | timestamp<sup>8+</sup> | number | 事件时间戳。 |
  | target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md) | 触发手势事件的元素对象显示区域。 |
  | source<sup>8+</sup> | [SourceType](ts-gesture-settings.md) | 事件输入设备。 |
Z
zengyawen 已提交
26 27


H
HelloCrease 已提交
28
## TouchObject对象说明
L
luoying_ace_admin 已提交
29 30

| 名称    | 类型                          | 描述                  |
H
HelloCrease 已提交
31
| ------- | --------------------------- | ------------------- |
K
kangchongtao 已提交
32
| type    | [TouchType](ts-appendix-enums.md#touchtype) | 触摸事件的类型。            |
H
HelloCrease 已提交
33 34 35 36 37
| id      | number                      | 手指唯一标识符。            |
| screenX | number                      | 触摸点相对于设备屏幕左边沿的X坐标。  |
| screenY | number                      | 触摸点相对于设备屏幕上边沿的Y坐标。  |
| x       | number                      | 触摸点相对于被触摸元素左边沿的X坐标。 |
| y       | number                      | 触摸点相对于被触摸元素上边沿的Y坐标。 |
Z
zengyawen 已提交
38 39

## 示例
Z
zengyawen 已提交
40

H
HelloCrease 已提交
41 42
```ts
// xxx.ets
Z
zengyawen 已提交
43 44 45 46 47 48 49 50
@Entry
@Component
struct TouchExample {
  @State text: string = ''
  @State eventType: string = ''

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Z
zengyawen 已提交
51
      Button('Touch').backgroundColor(0x2788D9).height(40).width(80)
Z
zengyawen 已提交
52 53 54 55 56 57 58 59 60 61
        .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 已提交
62
          console.info(this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
Z
zengyawen 已提交
63
          + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\ncomponent globalPos:('
64
          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
Z
zengyawen 已提交
65
          + event.target.area.width + '\nheight:' + event.target.area.height)
Z
zengyawen 已提交
66 67 68 69 70 71 72
        })
      Text(this.text)
    }.height(200).width(350).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
73
![zh-cn_image_0000001209874754](figures/zh-cn_image_0000001209874754.gif)