ts-basic-gestures-longpressgesture.md 2.2 KB
Newer Older
Z
zengyawen 已提交
1
# LongPressGesture
Z
zengyawen 已提交
2 3


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

7
The **&lt;LongPressGesture&gt;** component can be used to trigger a long press gesture.
Z
zengyawen 已提交
8 9

## Required Permissions
Z
zengyawen 已提交
10 11

None
Z
zengyawen 已提交
12

Z
zengyawen 已提交
13 14 15 16 17 18

## APIs

LongPressGesture(options?: { fingers?: number, repeat?: boolean, duration?: number })

- Parameters
19
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
20
  | -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
21 22 23
  | fingers | number | No | 1 | Minimum number of fingers to trigger a long press gesture. The value ranges from 1 to 10. |
  | repeat | boolean | No | false | Whether to continuously trigger the event callback. |
  | duration | number | No | 500 | Minimum hold-down time, in ms. |
Z
zengyawen 已提交
24 25 26 27


## Events

E
ester.zhou 已提交
28
| Name | Description |
Z
zengyawen 已提交
29
| -------- | -------- |
E
ester.zhou 已提交
30 31 32
| onAction((event?: GestureEvent) =&gt; void) | Callback invoked when a long press gesture is recognized. |
| onActionEnd((event?: GestureEvent) =&gt; void) | Callback invoked when the finger used for a long press gesture is lift. |
| onActionCancel(event: () =&gt; void) | Callback invoked when a tap cancellation event is received after a long press gesture is recognized. |
Z
zengyawen 已提交
33

E
ester.zhou 已提交
34
- GestureEvent attributes related to the long press gesture
35
  | Name | Type | Description |
Z
zengyawen 已提交
36
  | -------- | -------- | -------- |
E
ester.zhou 已提交
37
  | repeat | boolean | Whether the event is repeated. |
Z
zengyawen 已提交
38 39 40 41


## Example

Z
zengyawen 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

```
@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text('LongPress onAction:' + this.count)
    }
    .height(200).width(300).padding(60).border({ width:1 }).margin(30)
    .gesture(
      LongPressGesture({ repeat: true })
        // Repeatedly triggered when the long press gesture exists.
E
ester.zhou 已提交
57
        .onAction((event: GestureEvent) => {
Z
zengyawen 已提交
58 59 60 61 62 63 64 65 66 67 68
          if (event.repeat) { this.count++ }
        })
        // Triggered when the long press gesture ends.
        .onActionEnd(() => {
          this.count = 0
        })
    )
  }
}
```

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