單一手勢(shì)

2024-02-07 12:50 更新

點(diǎn)擊手勢(shì)(TapGesture)

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

點(diǎn)擊手勢(shì)支持單次點(diǎn)擊和多次點(diǎn)擊,擁有兩個(gè)可選參數(shù):

  • count:非必填參數(shù),聲明該點(diǎn)擊手勢(shì)識(shí)別的連續(xù)點(diǎn)擊次數(shù)。默認(rèn)值為1,若設(shè)置小于1的非法值會(huì)被轉(zhuǎn)化為默認(rèn)值。如果配置多次點(diǎn)擊,上一次抬起和下一次按下的超時(shí)時(shí)間為300毫秒。
  • fingers:非必填參數(shù),用于聲明觸發(fā)點(diǎn)擊的手指數(shù)量,最小值為1,最大值為10,默認(rèn)值為1。當(dāng)配置多指時(shí),若第一根手指按下300毫秒內(nèi)未有足夠的手指數(shù)按下則手勢(shì)識(shí)別失敗。當(dāng)實(shí)際點(diǎn)擊手指數(shù)超過配置值時(shí),手勢(shì)識(shí)別失敗。
    以在Text組件上綁定雙擊手勢(shì)(count值為2的點(diǎn)擊手勢(shì))為例:
    1. // xxx.ets
    2. @Entry
    3. @Component
    4. struct Index {
    5. @State value: string = "";
    6. build() {
    7. Column() {
    8. Text('Click twice').fontSize(28)
    9. .gesture(
    10. // 綁定count為2的TapGesture
    11. TapGesture({ count: 2 })
    12. .onAction((event: GestureEvent) => {
    13. this.value = JSON.stringify(event.fingerList[0]);
    14. }))
    15. Text(this.value)
    16. }
    17. .height(200)
    18. .width(250)
    19. .padding(20)
    20. .border({ width: 3 })
    21. .margin(30)
    22. }
    23. }

長(zhǎng)按手勢(shì)(LongPressGesture)

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

長(zhǎng)按手勢(shì)用于觸發(fā)長(zhǎng)按手勢(shì)事件,觸發(fā)長(zhǎng)按手勢(shì)的最少手指數(shù)量為1,最短長(zhǎng)按事件為500毫秒,擁有三個(gè)可選參數(shù):

  • fingers:非必選參數(shù),用于聲明觸發(fā)長(zhǎng)按手勢(shì)所需要的最少手指數(shù)量,最小值為1,最大值為10,默認(rèn)值為1。
  • repeat:非必選參數(shù),用于聲明是否連續(xù)觸發(fā)事件回調(diào),默認(rèn)值為false。
  • duration:非必選參數(shù),用于聲明觸發(fā)長(zhǎng)按所需的最短時(shí)間,單位為毫秒,默認(rèn)值為500。

以在Text組件上綁定可以重復(fù)觸發(fā)的長(zhǎng)按手勢(shì)為例:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State count: number = 0;
  6. build() {
  7. Column() {
  8. Text('LongPress OnAction:' + this.count).fontSize(28)
  9. .gesture(
  10. // 綁定可以重復(fù)觸發(fā)的LongPressGesture
  11. LongPressGesture({ repeat: true })
  12. .onAction((event: GestureEvent) => {
  13. if (event.repeat) {
  14. this.count++;
  15. }
  16. })
  17. .onActionEnd(() => {
  18. this.count = 0;
  19. })
  20. )
  21. }
  22. .height(200)
  23. .width(250)
  24. .padding(20)
  25. .border({ width: 3 })
  26. .margin(30)
  27. }
  28. }

拖動(dòng)手勢(shì)(PanGesture)

  1. PanGesture(value?:{ fingers?:number; direction?:PanDirection; distance?:number})

拖動(dòng)手勢(shì)用于觸發(fā)拖動(dòng)手勢(shì)事件,滑動(dòng)達(dá)到最小滑動(dòng)距離(默認(rèn)值為5vp)時(shí)拖動(dòng)手勢(shì)識(shí)別成功,擁有三個(gè)可選參數(shù):

  • fingers:非必選參數(shù),用于聲明觸發(fā)拖動(dòng)手勢(shì)所需要的最少手指數(shù)量,最小值為1,最大值為10,默認(rèn)值為1。
  • direction:非必選參數(shù),用于聲明觸發(fā)拖動(dòng)的手勢(shì)方向,此枚舉值支持邏輯與(&)和邏輯或(|)運(yùn)算。默認(rèn)值為Pandirection.All。
  • distance:非必選參數(shù),用于聲明觸發(fā)拖動(dòng)的最小拖動(dòng)識(shí)別距離,單位為vp,默認(rèn)值為5。

以在Text組件上綁定拖動(dòng)手勢(shì)為例,可以通過在拖動(dòng)手勢(shì)的回調(diào)函數(shù)中修改組件的布局位置信息來(lái)實(shí)現(xiàn)組件的拖動(dòng):

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State offsetX: number = 0;
  6. @State offsetY: number = 0;
  7. @State positionX: number = 0;
  8. @State positionY: number = 0;
  9. build() {
  10. Column() {
  11. Text('PanGesture Offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
  12. .fontSize(28)
  13. .height(200)
  14. .width(300)
  15. .padding(20)
  16. .border({ width: 3 })
  17. // 在組件上綁定布局位置信息
  18. .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
  19. .gesture(
  20. // 綁定拖動(dòng)手勢(shì)
  21. PanGesture()
  22. .onActionStart((event: GestureEvent) => {
  23. console.info('Pan start');
  24. })
  25. // 當(dāng)觸發(fā)拖動(dòng)手勢(shì)時(shí),根據(jù)回調(diào)函數(shù)修改組件的布局位置信息
  26. .onActionUpdate((event: GestureEvent) => {
  27. this.offsetX = this.positionX + event.offsetX;
  28. this.offsetY = this.positionY + event.offsetY;
  29. })
  30. .onActionEnd(() => {
  31. this.positionX = this.offsetX;
  32. this.positionY = this.offsetY;
  33. })
  34. )
  35. }
  36. .height(200)
  37. .width(250)
  38. }
  39. }

說(shuō)明

大部分可滑動(dòng)組件,如List、Grid、Scroll、Tab等組件是通過PanGesture實(shí)現(xiàn)滑動(dòng),在組件內(nèi)部的子組件綁定拖動(dòng)手勢(shì)(PanGesture)或者滑動(dòng)手勢(shì)(SwipeGesture)會(huì)導(dǎo)致手勢(shì)競(jìng)爭(zhēng)。

當(dāng)在子組件綁定PanGesture時(shí),在子組件區(qū)域進(jìn)行滑動(dòng)僅觸發(fā)子組件的PanGesture。如果需要父組件響應(yīng),需要通過修改手勢(shì)綁定方法或者子組件向父組件傳遞消息進(jìn)行實(shí)現(xiàn),或者通過修改父子組件的PanGesture參數(shù)distance使得拖動(dòng)更靈敏。當(dāng)子組件綁定SwipeGesture時(shí),由于PanGesture和SwipeGesture觸發(fā)條件不同,需要修改PanGesture和SwipeGesture的參數(shù)以達(dá)到所需效果。

捏合手勢(shì)(PinchGesture)

  1. PinchGesture(value?:{fingers?:number; distance?:number})

捏合手勢(shì)用于觸發(fā)捏合手勢(shì)事件,觸發(fā)捏合手勢(shì)的最少手指數(shù)量為2指,最大為5指,最小識(shí)別距離為5vp,擁有兩個(gè)可選參數(shù):

  • fingers:非必選參數(shù),用于聲明觸發(fā)捏合手勢(shì)所需要的最少手指數(shù)量,最小值為2,最大值為5,默認(rèn)值為2。
  • distance:非必選參數(shù),用于聲明觸發(fā)捏合手勢(shì)的最小距離,單位為vp,默認(rèn)值為5。

以在Column組件上綁定三指捏合手勢(shì)為例,可以通過在捏合手勢(shì)的函數(shù)回調(diào)中獲取縮放比例,實(shí)現(xiàn)對(duì)組件的縮小或放大:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  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. .border({ width: 3 })
  18. .margin({ top: 100 })
  19. // 在組件上綁定縮放比例,可以通過修改縮放比例來(lái)實(shí)現(xiàn)組件的縮小或者放大
  20. .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
  21. .gesture(
  22. // 在組件上綁定三指觸發(fā)的捏合手勢(shì)
  23. PinchGesture({ fingers: 3 })
  24. .onActionStart((event: GestureEvent) => {
  25. console.info('Pinch start');
  26. })
  27. // 當(dāng)捏合手勢(shì)觸發(fā)時(shí),可以通過回調(diào)函數(shù)獲取縮放比例,從而修改組件的縮放比例
  28. .onActionUpdate((event: GestureEvent) => {
  29. this.scaleValue = this.pinchValue * event.scale;
  30. this.pinchX = event.pinchCenterX;
  31. this.pinchY = event.pinchCenterY;
  32. })
  33. .onActionEnd(() => {
  34. this.pinchValue = this.scaleValue;
  35. console.info('Pinch end');
  36. })
  37. )
  38. }
  39. }
  40. }

旋轉(zhuǎn)手勢(shì)(RotationGesture)

  1. RotationGesture(value?:{fingers?:number; angle?:number})

旋轉(zhuǎn)手勢(shì)用于觸發(fā)旋轉(zhuǎn)手勢(shì)事件,觸發(fā)旋轉(zhuǎn)手勢(shì)的最少手指數(shù)量為2指,最大為5指,最小改變度數(shù)為1度,擁有兩個(gè)可選參數(shù):

  • fingers:非必選參數(shù),用于聲明觸發(fā)旋轉(zhuǎn)手勢(shì)所需要的最少手指數(shù)量,最小值為2,最大值為5,默認(rèn)值為2。
  • angle:非必選參數(shù),用于聲明觸發(fā)旋轉(zhuǎn)手勢(shì)的最小改變度數(shù),單位為deg,默認(rèn)值為1。

以在Text組件上綁定旋轉(zhuǎn)手勢(shì)實(shí)現(xiàn)組件的旋轉(zhuǎn)為例,可以通過在旋轉(zhuǎn)手勢(shì)的回調(diào)函數(shù)中獲取旋轉(zhuǎn)角度,從而實(shí)現(xiàn)組件的旋轉(zhuǎn):

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State angle: number = 0;
  6. @State rotateValue: number = 0;
  7. build() {
  8. Column() {
  9. Text('RotationGesture angle:' + this.angle).fontSize(28)
  10. // 在組件上綁定旋轉(zhuǎn)布局,可以通過修改旋轉(zhuǎn)角度來(lái)實(shí)現(xiàn)組件的旋轉(zhuǎn)
  11. .rotate({ angle: this.angle })
  12. .gesture(
  13. RotationGesture()
  14. .onActionStart((event: GestureEvent) => {
  15. console.info('RotationGesture is onActionStart');
  16. })
  17. // 當(dāng)旋轉(zhuǎn)手勢(shì)生效時(shí),通過旋轉(zhuǎn)手勢(shì)的回調(diào)函數(shù)獲取旋轉(zhuǎn)角度,從而修改組件的旋轉(zhuǎn)角度
  18. .onActionUpdate((event: GestureEvent) => {
  19. this.angle = this.rotateValue + event.angle;
  20. console.info('RotationGesture is onActionEnd');
  21. })
  22. // 當(dāng)旋轉(zhuǎn)結(jié)束抬手時(shí),固定組件在旋轉(zhuǎn)結(jié)束時(shí)的角度
  23. .onActionEnd(() => {
  24. this.rotateValue = this.angle;
  25. console.info('RotationGesture is onActionEnd');
  26. })
  27. .onActionCancel(() => {
  28. console.info('RotationGesture is onActionCancel');
  29. })
  30. )
  31. }
  32. .height(200)
  33. .width(250)
  34. }
  35. }

滑動(dòng)手勢(shì)(SwipeGesture)

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

滑動(dòng)手勢(shì)用于觸發(fā)滑動(dòng)事件,當(dāng)滑動(dòng)速度大于100vp/s時(shí)可以識(shí)別成功,擁有三個(gè)可選參數(shù):

  • fingers:非必選參數(shù),用于聲明觸發(fā)滑動(dòng)手勢(shì)所需要的最少手指數(shù)量,最小值為1,最大值為10,默認(rèn)值為1。
  • direction:非必選參數(shù),用于聲明觸發(fā)滑動(dòng)手勢(shì)的方向,此枚舉值支持邏輯與(&)和邏輯或(|)運(yùn)算。默認(rèn)值為SwipeDirection.All。
  • speed:非必選參數(shù),用于聲明觸發(fā)滑動(dòng)的最小滑動(dòng)識(shí)別速度,單位為vp/s,默認(rèn)值為100。

以在Column組件上綁定滑動(dòng)手勢(shì)實(shí)現(xiàn)組件的旋轉(zhuǎn)為例:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Index {
  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. // 在Column組件上綁定旋轉(zhuǎn),通過滑動(dòng)手勢(shì)的滑動(dòng)速度和角度修改旋轉(zhuǎn)的角度
  18. .rotate({ angle: this.rotateAngle })
  19. .gesture(
  20. // 綁定滑動(dòng)手勢(shì)且限制僅在豎直方向滑動(dòng)時(shí)觸發(fā)
  21. SwipeGesture({ direction: SwipeDirection.Vertical })
  22. // 當(dāng)滑動(dòng)手勢(shì)觸發(fā)時(shí),獲取滑動(dòng)的速度和角度,實(shí)現(xiàn)對(duì)組件的布局參數(shù)的修改
  23. .onAction((event: GestureEvent) => {
  24. this.speed = event.speed;
  25. this.rotateAngle = event.angle;
  26. })
  27. )
  28. }
  29. }
  30. }

說(shuō)明

當(dāng)SwipeGesture和PanGesture同時(shí)綁定時(shí),若二者是以默認(rèn)方式或者互斥方式進(jìn)行綁定時(shí),會(huì)發(fā)生競(jìng)爭(zhēng)。SwipeGesture的觸發(fā)條件為滑動(dòng)速度達(dá)到100vp/s,PanGesture的觸發(fā)條件為滑動(dòng)距離達(dá)到5vp,先達(dá)到觸發(fā)條件的手勢(shì)觸發(fā)。可以通過修改SwipeGesture和PanGesture的參數(shù)以達(dá)到不同的效果。

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)