組合手勢(shì)

2024-01-22 16:54 更新

手勢(shì)識(shí)別組合,即多種手勢(shì)組合為復(fù)合手勢(shì),支持連續(xù)識(shí)別、并行識(shí)別和互斥識(shí)別。

說(shuō)明

從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

接口

GestureGroup(mode: GestureMode, ...gesture: GestureType[])

  • 參數(shù)

    參數(shù)名

    參數(shù)類型

    必填

    默認(rèn)值

    參數(shù)描述

    mode

    GestureMode

    -

    設(shè)置組合手勢(shì)識(shí)別模式。

    gesture

    TapGesture

    LongPressGesture

    PanGesture

    PinchGesture

    RotationGesture

    -

    設(shè)置1個(gè)或者多個(gè)基礎(chǔ)手勢(shì)類型時(shí),這些手勢(shì)會(huì)被識(shí)別為組合手勢(shì)。若此參數(shù)不填則組合手勢(shì)識(shí)別功能不生效。

    說(shuō)明:

    當(dāng)需要為一個(gè)組件同時(shí)添加單擊和雙擊手勢(shì)時(shí),可在組合手勢(shì)中添加兩個(gè)TapGesture,需要雙擊手勢(shì)在前,單擊手勢(shì)在后,否則不生效。

GestureMode枚舉說(shuō)明

名稱

描述

Sequence

順序識(shí)別,按照手勢(shì)的注冊(cè)順序識(shí)別手勢(shì),直到所有手勢(shì)識(shí)別成功。若有一個(gè)手勢(shì)識(shí)別失敗,后續(xù)手勢(shì)識(shí)別均失敗。

Parallel

并發(fā)識(shí)別,注冊(cè)的手勢(shì)同時(shí)識(shí)別,直到所有手勢(shì)識(shí)別結(jié)束,手勢(shì)識(shí)別互相不影響。

Exclusive

互斥識(shí)別,注冊(cè)的手勢(shì)同時(shí)識(shí)別,若有一個(gè)手勢(shì)識(shí)別成功,則結(jié)束手勢(shì)識(shí)別。

事件

名稱

功能描述

onCancel(event: () => void)

順序組合手勢(shì)(GestureMode.Sequence)取消后觸發(fā)回調(diào)。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct GestureGroupExample {
  5. @State count: number = 0
  6. @State offsetX: number = 0
  7. @State offsetY: number = 0
  8. @State positionX: number = 0
  9. @State positionY: number = 0
  10. @State borderStyles: BorderStyle = BorderStyle.Solid
  11. build() {
  12. Column() {
  13. Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
  14. .fontSize(15)
  15. }
  16. .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
  17. .height(150)
  18. .width(200)
  19. .padding(20)
  20. .margin(20)
  21. .border({ width: 3, style: this.borderStyles })
  22. .gesture(
  23. // 以下組合手勢(shì)為順序識(shí)別,當(dāng)長(zhǎng)按手勢(shì)事件未正常觸發(fā)時(shí)則不會(huì)觸發(fā)拖動(dòng)手勢(shì)事件
  24. GestureGroup(GestureMode.Sequence,
  25. LongPressGesture({ repeat: true })
  26. .onAction((event: GestureEvent) => {
  27. if (event.repeat) {
  28. this.count++
  29. }
  30. console.info('LongPress onAction')
  31. })
  32. .onActionEnd(() => {
  33. console.info('LongPress end')
  34. }),
  35. PanGesture()
  36. .onActionStart(() => {
  37. this.borderStyles = BorderStyle.Dashed
  38. console.info('pan start')
  39. })
  40. .onActionUpdate((event: GestureEvent) => {
  41. this.offsetX = this.positionX + event.offsetX
  42. this.offsetY = this.positionY + event.offsetY
  43. console.info('pan update')
  44. })
  45. .onActionEnd(() => {
  46. this.positionX = this.offsetX
  47. this.positionY = this.offsetY
  48. this.borderStyles = BorderStyle.Solid
  49. console.info('pan end')
  50. })
  51. )
  52. .onCancel(() => {
  53. console.info('sequence gesture canceled')
  54. })
  55. )
  56. }
  57. }

示意圖:

按順序首先觸發(fā)長(zhǎng)按事件:

按順序首先觸發(fā)長(zhǎng)按事件,長(zhǎng)按事件識(shí)別結(jié)束之后,其次觸發(fā)拖動(dòng)事件,向右下方拖動(dòng):

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)