觸發(fā)捏合手勢

2024-01-22 16:49 更新

用于觸發(fā)捏合手勢,觸發(fā)捏合手勢的最少手指為2指,最大為5指,最小識別距離為5vp。

說明

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

接口

PinchGesture(value?: { fingers?: number, distance?: number })

參數:

參數名稱

參數類型

必填

參數描述

fingers

number

觸發(fā)捏合的最少手指數, 最小為2指,最大為5指。

默認值:2

distance

number

最小識別距離,單位為vp。

默認值:5

事件

名稱

功能描述

onActionStart(event:(event?: GestureEvent) => void)

Pinch手勢識別成功回調。

onActionUpdate(event:(event?: GestureEvent) => void)

Pinch手勢移動過程中回調。

onActionEnd(event:(event?: GestureEvent) => void)

Pinch手勢識別成功,手指抬起后觸發(fā)回調。

onActionCancel(event: () => void)

Pinch手勢識別成功,接收到觸摸取消事件觸發(fā)回調。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct PinchGestureExample {
  5. @State scaleValue: number = 1
  6. @State pinchValue: number = 1
  7. @State pinchX: number = 0
  8. @State pinchY: number = 0
  9. build() {
  10. Column() {
  11. Column() {
  12. Text('PinchGesture scale:\n' + this.scaleValue)
  13. Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')
  14. }
  15. .height(200)
  16. .width(300)
  17. .padding(20)
  18. .border({ width: 3 })
  19. .margin({ top: 100 })
  20. .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
  21. // 三指捏合觸發(fā)該手勢事件
  22. .gesture(
  23. PinchGesture({ fingers: 3 })
  24. .onActionStart((event: GestureEvent) => {
  25. console.info('Pinch start')
  26. })
  27. .onActionUpdate((event: GestureEvent) => {
  28. this.scaleValue = this.pinchValue * event.scale
  29. this.pinchX = event.pinchCenterX
  30. this.pinchY = event.pinchCenterY
  31. })
  32. .onActionEnd(() => {
  33. this.pinchValue = this.scaleValue
  34. console.info('Pinch end')
  35. })
  36. )
  37. }.width('100%')
  38. }
  39. }

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號