Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Handling Compound Gestures in ArkUI for HarmonyOS

Tech 1

1. Sequantial Recognition

Sequential cmopound gestures require gestures to trigger in a specific order; subsequent gestures activate only after the previous one completes successfully.

// xxx.ets
@Entry
@Component
struct MyGestureDemo {
  @State moveX: number = 0;
  @State moveY: number = 0;
  @State pressCount: number = 0;
  @State lastX: number = 0;
  @State lastY: number = 0;
  @State borderStyle: BorderStyle = BorderStyle.Solid

  build() {
    Column() {
      Text(`Sequential Gesture\nLong Press Count: ${this.pressCount}\nPan Offset:\nX: ${this.moveX}\nY: ${this.moveY}`)
        .fontSize(28)
    }
    .translate({ x: this.moveX, y: this.moveY, z: 0 })
    .height(250)
    .width(300)
    .gesture(
      GestureGroup(GestureMode.Sequence,
        LongPressGesture({ repeat: true })
          .onAction((event: GestureEvent) => {
            if (event.repeat) {
              this.pressCount++;
            }
            console.info('LongPress activated');
          })
          .onActionEnd(() => {
            console.info('LongPress ended');
          }),
        PanGesture()
          .onActionStart(() => {
            this.borderStyle = BorderStyle.Dashed;
            console.info('Pan started');
          })
          .onActionUpdate((event: GestureEvent) => {
            this.moveX = this.lastX + event.offsetX;
            this.moveY = this.lastY + event.offsetY;
            console.info('Pan updated');
          })
          .onActionEnd(() => {
            this.lastX = this.moveX;
            this.lastY = this.moveY;
            this.borderStyle = BorderStyle.Solid;
          })
      )
    )
  }
}

2. Parallel Recognition

Parallel compound gestures detect and activate multiple gestures simultaneously. This allows overlapping interactions for a more flexible user experience.

// xxx.ets
@Entry
@Component
struct ParallelGestureDemo {
  @State singleTapCount: number = 0;
  @State doubleTapCount: number = 0;

  build() {
    Column() {
      Text(`Parallel Gesture\nSingle Tap Count: ${this.singleTapCount}\nDouble Tap Count: ${this.doubleTapCount}`)
        .fontSize(28)
    }
    .height(200)
    .width(250)
    .gesture(
      GestureGroup(GestureMode.Parallel,
        TapGesture({ count: 1 })
          .onAction(() => {
            this.singleTapCount++;
          }),
        TapGesture({ count: 2 })
          .onAction(() => {
            this.doubleTapCount++;
          })
      )
    )
  }
}

3. Exclusive Recongition

Exclusive compound gestures allow only one gesture to activate from a group of conflicting gestures. The system selects the first valid gesture and ignores others.

// xxx.ets
@Entry
@Component
struct ExclusiveGestureDemo {
  @State singleHitCount: number = 0;
  @State doubleHitCount: number = 0;

  build() {
    Column() {
      Text(`Exclusive Gesture\nSingle Tap Count: ${this.singleHitCount}\nDouble Tap Count: ${this.doubleHitCount}`)
        .fontSize(28)
    }
    .height(200)
    .width(250)
    .gesture(
      GestureGroup(GestureMode.Exclusive,
        TapGesture({ count: 1 })
          .onAction(() => {
            this.singleHitCount++;
          }),
        TapGesture({ count: 2 })
          .onAction(() => {
            this.doubleHitCount++;
          })
      )
    )
  }
}
Tags: HarmonyOS

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.