IT评测·应用市场-qidao123.com

标题: HarmonyOS手势处理 [打印本页]

作者: 铁佛    时间: 2025-1-24 15:39
标题: HarmonyOS手势处理
手势处理

手势事件


绑定手势的方法

. gesture


  1. .gesture(gestureType,mak?:GestureMask)
复制代码
. priorityGesture


  1. .priorityGesture(gestureType,mask?:GestureMask)
复制代码

.parallelGesture


  1. .parallelGesture(gestureType,mask?:GestureMask)
复制代码

按照手势数量分

单一手势

点击手势(TapGesture)

长按手势(LongPressGesture)

拖动手势(PanGesture)

捏合手势(PinchGesture)

旋转手势(RotationGesture)

滑动手势(SwiperGesture)

组合手势


顺序辨认


  1. Column(){
  2.   Text(
  3.     '组合手势\n' + '长按手势:' + this.count + '\n拖动手势偏移' +
  4.       '\n x:' + this.x + '\ny:' + this.y
  5.   )
  6.     .fontSize(28)
  7. }
  8. .width(250)
  9. .height(300)
  10. .margin(10)
  11. .border({
  12.   width:1,
  13.   style:this.border_style
  14. })
  15. //偏移量
  16. .translate({
  17.   x:this.x,
  18.   y:this.y
  19. })
  20. /*
  21. * 一个有长按手势与拖动手势结合的组合手势
  22. *
  23. * */
  24. //绑定手势
  25. .gesture(
  26.   //组合手势
  27.   GestureGroup(
  28.     //声明组合手势类型为顺序识别
  29.     GestureMode.Sequence,
  30.     //第一个触发的手势为长按手势,且长按手势可以多次相应
  31.     LongPressGesture( { repeat: true } )
  32.       .onAction((event) => {
  33.         this.count++
  34.       }),
  35.     //当长按之后进行拖动,拖动手势将被触发
  36.     PanGesture()
  37.       .onActionStart(() => {
  38.         this.border_style = BorderStyle.Dashed
  39.       })
  40.       .onActionUpdate((event) => {
  41.         this.x = this.offset_x + event.offsetX
  42.         this.y = this.offset_y + event.offsetY
  43.       })
  44.       .onActionEnd(() => {
  45.         this.offset_x = this.x
  46.         this.offset_y = this.y
  47.         this.border_style = BorderStyle.Solid
  48.       })
  49.   )
  50.     .onCancel(() => {
  51.       //顺序手势没有全部执行则触发
  52.       console.log('顺序手势结束')
  53.     })
  54. )
复制代码
并行辨认


  1. Column(){
  2.   Text('并行事件\n' + '并行事件1:' + this.count1 +
  3.     '\n 并行事件2:' + this.count2
  4.   )
  5.     .fontSize(28)
  6. }
  7. .height(500)
  8. .width('100%')
  9. .border({
  10.   width:1
  11. })
  12. .justifyContent(FlexAlign.Center)
  13. //绑定手势
  14. .gesture(
  15.   //并行识别的组合手势
  16.   GestureGroup(
  17.     GestureMode.Parallel,
  18.     //单击手势
  19.     TapGesture({ count:1 })
  20.       .onAction(() => {
  21.         this.count1++
  22.       }),
  23.     //双击手势
  24.     TapGesture({ count:2 })
  25.       .onAction(() => {
  26.         this.count2++
  27.       })
  28.   )
  29. )
复制代码
互斥辨认


  1. Column(){
  2.   Text('互斥事件\n' + '互斥事件1:' + this.count1 +
  3.     '\n 互斥事件2:' + this.count2
  4.   )
  5.     .fontSize(28)
  6. }
  7. .height(500)
  8. .width('100%')
  9. .border({
  10.   width:1
  11. })
  12. .justifyContent(FlexAlign.Center)
  13. //绑定手势
  14. .gesture(
  15.   //并行识别的组合手势
  16.   GestureGroup(
  17.     GestureMode.Exclusive,
  18.     //为什么?因为点击事件以两次点击间隔时间
  19.     // 是否大于300ms为识别界限,
  20.     // 如果小于300ms那么继续计入到点击次数中
  21.     //如果大于300ms触发对应事件
  22.     //双击手势
  23.     TapGesture({ count:4 })
  24.       .onAction(() => {
  25.         this.count2++
  26.       }),
  27.     //单击手势
  28.     TapGesture({ count:3 })
  29.       .onAction(() => {
  30.         this.count1++
  31.       }),
  32.   )
  33. )
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4