觸發(fā)滑動事件

2024-01-22 16:50 更新

用于觸發(fā)滑動事件,滑動速度大于100vp/s時可識別成功。

說明

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

接口

SwipeGesture(value?: { fingers?: number; direction?: SwipeDirection; speed?: number })

參數(shù):

參數(shù)名稱參數(shù)類型必填參數(shù)描述
fingersnumber

觸發(fā)滑動的最少手指數(shù),默認(rèn)為1,最小為1指,最大為10指。

默認(rèn)值:1

directionSwipeDirection

觸發(fā)滑動手勢的滑動方向。

默認(rèn)值:SwipeDirection.All

speednumber

識別滑動的最小速度(默認(rèn)為100VP/秒)。

默認(rèn)值:100

SwipeDirection枚舉說明

名稱描述
All所有方向。
Horizontal水平方向,手指滑動方向與x軸夾角小于45度時觸發(fā)。
Vertical豎直方向,手指滑動方向與y軸夾角小于45度時觸發(fā)。
None任何方向均不可觸發(fā)。

事件

名稱功能描述
onAction(event:(event?: GestureEvent) => void)滑動手勢識別成功回調(diào)。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct SwipeGestureExample {
  5. @State rotateAngle: number = 0
  6. @State speed: number = 1
  7. build() {
  8. Column() {
  9. Column() {
  10. Text("SwipeGesture speed\n" + this.speed)
  11. Text("SwipeGesture angle\n" + this.rotateAngle)
  12. }
  13. .border({ width: 3 })
  14. .width(300)
  15. .height(200)
  16. .margin(100)
  17. .rotate({ angle: this.rotateAngle })
  18. // 單指豎直方向滑動時觸發(fā)該事件
  19. .gesture(
  20. SwipeGesture({ direction: SwipeDirection.Vertical })
  21. .onAction((event: GestureEvent) => {
  22. this.speed = event.speed
  23. this.rotateAngle = event.angle
  24. })
  25. )
  26. }.width('100%')
  27. }
  28. }

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號