HarmonyOS/OpenHarmony (Stage model) application development single gesture (1)

1. TapGesture

TapGesture(value?:{ count?:number; fingers?:number})

Click gesture supports single click and multiple clicks, and has two optional parameters:

count: A non-required parameter that declares the number of consecutive clicks recognized by this click gesture. The default value is 1. If an illegal value less than 1 is set, it will be converted to the default value. If multiple clicks are configured, the timeout between the last lift and the next press is 300 milliseconds.

Fingers: Non-required parameter, used to declare the number of fingers that trigger the click. The minimum value is 1, the maximum value is 10, and the default value is 1. When multiple fingers are configured, gesture recognition will fail if there are not enough fingers to press within 300 milliseconds of pressing the first finger. When the actual number of finger clicks exceeds the configured value, gesture recognition fails.

Take binding a double-click gesture (click gesture with count value 2) on the Text component as an example:

// xxx.ets
@Entry
@Component
struct Index {
  @State value: string = "";
  
  build() {
    Column() {
      Text('Click twice').fontSize(28)
        .gesture(
          // 绑定count为2的TapGesture
          TapGesture({ count: 2 })
            .onAction((event: GestureEvent) => {
              this.value = JSON.stringify(event.fingerList[0]);
            }))
      Text(this.value)
    }
    .height(200)
    .width(250)
    .padding(20)
    .border({ width: 3 })
    .margin(30)
  }
}

2. Long press gesture (LongPressGesture)

  1. LongPressGesture(value?:{ fingers?:number; repeat?:boolean; duration?:number})

The long press gesture is used to trigger the long press gesture event. The minimum number of fingers to trigger the long press gesture is 1, and the shortest long press event is 500 milliseconds. It has three optional parameters:

fingers: optional parameter, used to declare the minimum number of fingers required to trigger the long press gesture. The minimum value is 1, the maximum value is 10, and the default value is 1.

repeat: optional parameter, used to declare whether to trigger event callbacks continuously. The default value is false.

duration: optional parameter, used to declare the minimum time required to trigger a long press, in milliseconds, and the default value is 500.

Take binding a long press gesture that can be triggered repeatedly on the Text component as an example:

// xxx.ets
@Entry
@Component
struct Index {
  @State count: number = 0;

  build() {
    Column() {
      Text('LongPress OnAction:' + this.count).fontSize(28)
        .gesture(
          // 绑定可以重复触发的LongPressGesture
          LongPressGesture({ repeat: true })
            .onAction((event: GestureEvent) => {
              if (event.repeat) {
                this.count++;
              }
            })
            .onActionEnd(() => {
              this.count = 0;
            })
        )
    }
    .height(200)
    .width(250)
    .padding(20)
    .border({ width: 3 })
    .margin(30)
  }
}

Guess you like

Origin blog.csdn.net/weixin_69135651/article/details/132598612