ts-universal-events-touch.md 3.6 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
  | Name               | Type                                      | Description          |
  | ------------------- | ---------------------------------------- | ------------ |
E
esterzhou 已提交
27
  | type                | [TouchType](ts-appendix-enums.md#touchtype)                                | Type of the touch event.    |
E
ester.zhou 已提交
28 29 30 31 32
  | 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
## TouchObject
| Name   | Type                         | Description                 |
| ------- | --------------------------- | ------------------- |
E
esterzhou 已提交
43
| type    | [TouchType](ts-appendix-enums.md#touchtype) | Type of the touch event.           |
E
ester.zhou 已提交
44 45 46 47 48
| 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 51

## Example

E
ester.zhou 已提交
52 53
```ts
// xxx.ets
Z
zengyawen 已提交
54 55 56 57 58 59 60 61
@Entry
@Component
struct TouchExample {
  @State text: string = ''
  @State eventType: string = ''

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

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