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


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


## Required Permissions
Z
zengyawen 已提交
9 10 11

None

Z
zengyawen 已提交
12 13 14 15 16

## Events

| Name | Pop-up | Description |
| -------- | -------- | -------- |
E
esterzhou 已提交
17
| onTouch(callback: (event?: TouchEvent) =&gt; void) | Yes | Invoked when a touch action is triggered. For details about the event parameters, see [TouchEvent](#touchevent). |
Z
zengyawen 已提交
18 19 20 21 22


### TouchEvent

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

- APIs
E
ester.zhou 已提交
32
  | Name | Description |
Z
zengyawen 已提交
33
  | -------- | -------- |
E
ester.zhou 已提交
34
  | stopPropagation(): void | Pop-up of the stop event. |
Z
zengyawen 已提交
35 36 37


- TouchObject
E
ester.zhou 已提交
38
  | Name | Type | Description |
Z
zengyawen 已提交
39
  | -------- | -------- | -------- |
E
ester.zhou 已提交
40 41 42 43 44 45
  | type | TouchType | Type of a 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 to touch. |
  | y | number | Y coordinate of the touch point relative to the upper edge of the element to touch. |
Z
zengyawen 已提交
46 47 48


- TouchType
E
ester.zhou 已提交
49
  | Name | Description |
Z
zengyawen 已提交
50
  | -------- | -------- |
E
ester.zhou 已提交
51 52 53 54
  | Down | Trigger a touch event when a finger is pressed. |
  | Up | Trigger a touch event when a finger is lifted. |
  | Move | Trigger a touch event when a finger moves on the screen in pressed state. |
  | Cancel | Trigger an event when a touch event is canceled. |
Z
zengyawen 已提交
55 56 57 58


## Example

Z
zengyawen 已提交
59

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

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

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