ts-universal-events-touch.md 3.8 KB
Newer Older
Z
zengyawen 已提交
1
# Touch Event
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
A touch event is triggered when a finger is pressed, slides, or is lifted from a component.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
E
ester.zhou 已提交
7
> This event is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10


## Required Permissions
Z
zengyawen 已提交
11 12 13

None

Z
zengyawen 已提交
14 15 16

## Events

E
ester.zhou 已提交
17 18 19
| Name                                      | Bubbling Supported| Description                                    |
| ---------------------------------------- | ---- | ---------------------------------------- |
| onTouch(callback: (event?: TouchEvent) => void) | Yes   | Invoked when a touch action is triggered. For details about **event**, see [TouchEvent](#touchevent).|
Z
zengyawen 已提交
20 21


E
ester.zhou 已提交
22
## TouchEvent
Z
zengyawen 已提交
23 24

- Attributes
E
ester.zhou 已提交
25 26 27 28 29 30 31 32
  | Name               | Type                                      | Description          |
  | ------------------- | ---------------------------------------- | ------------ |
  | type                | TouchType                                | Type of the touch event.    |
  | touches             | Array<[TouchObject](#touchobject)> | All finger information.     |
  | changedTouches      | Array<[TouchObject](#touchobject)> | Finger information changed.|
  | timestamp           | number                                   | Timestamp of the event.      |
  | target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md#eventtarget8) | Target of the event.    |

Z
zengyawen 已提交
33 34

- APIs
E
ester.zhou 已提交
35 36 37
  | Name                  | Description   |
  | ---------------------- | ------- |
  | stopPropagation(): void| Stops the event from bubbling upwards or downwards.|
Z
zengyawen 已提交
38 39


E
ester.zhou 已提交
40 41 42 43 44 45 46 47 48
## TouchObject
| Name   | Type                         | Description                 |
| ------- | --------------------------- | ------------------- |
| type    | [TouchType](#touchtype-enums) | Type of the touch event.           |
| id      | number                      | Unique identifier of a finger.           |
| screenX | number                      | X-coordinate of the touch point relative to the left edge of the screen. |
| screenY | number                      | Y-coordinate of the touch point relative to the upper edge of the device screen. |
| x       | number                      | X-coordinate of the touch point relative to the left edge of the element being touched. |
| y       | number                      | Y-coordinate of the touch point relative to the upper edge of the element being touched. |
Z
zengyawen 已提交
49 50


E
ester.zhou 已提交
51 52 53 54 55 56 57
## TouchType Enums
| Name    | Description             |
| ------ | --------------- |
| Down   | A finger is pressed.       |
| Up     | A finger is lifted.       |
| Move   | A finger moves on the screen in pressed state.|
| Cancel | A touch event is canceled.     |
Z
zengyawen 已提交
58 59 60 61


## Example

E
ester.zhou 已提交
62 63
```ts
// xxx.ets
Z
zengyawen 已提交
64 65 66 67 68 69 70 71
@Entry
@Component
struct TouchExample {
  @State text: string = ''
  @State eventType: string = ''

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Z
zengyawen 已提交
72
      Button('Touch').backgroundColor(0x2788D9).height(40).width(80)
Z
zengyawen 已提交
73 74 75 76 77 78 79 80 81 82
        .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 已提交
83
          console.info(this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
Z
zengyawen 已提交
84
          + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\ncomponent globalPos:('
E
ester.zhou 已提交
85
          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
Z
zengyawen 已提交
86
          + event.target.area.width + '\nheight:' + event.target.area.height)
Z
zengyawen 已提交
87 88 89 90 91 92 93
        })
      Text(this.text)
    }.height(200).width(350).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
94
![en-us_image_0000001212058464](figures/en-us_image_0000001212058464.gif)