单一手势

2024-02-07 12:50 更新

点击手势(TapGesture)

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

点击手势支持单次点击和多次点击,拥有两个可选参数:

  • count:非必填参数,声明该点击手势识别的连续点击次数。默认值为1,若设置小于1的非法值会被转化为默认值。如果配置多次点击,上一次抬起和下一次按下的超时时间为300毫秒。
  • fingers:非必填参数,用于声明触发点击的手指数量,最小值为1,最大值为10,默认值为1。当配置多指时,若第一根手指按下300毫秒内未有足够的手指数按下则手势识别失败。当实际点击手指数超过配置值时,手势识别失败。
    以在Text组件上绑定双击手势(count值为2的点击手势)为例:
    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. }

长按手势(LongPressGesture)

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

长按手势用于触发长按手势事件,触发长按手势的最少手指数量为1,最短长按事件为500毫秒,拥有三个可选参数:

  • fingers:非必选参数,用于声明触发长按手势所需要的最少手指数量,最小值为1,最大值为10,默认值为1。
  • repeat:非必选参数,用于声明是否连续触发事件回调,默认值为false。
  • duration:非必选参数,用于声明触发长按所需的最短时间,单位为毫秒,默认值为500。

以在Text组件上绑定可以重复触发的长按手势为例:

  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. // 绑定可以重复触发的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. }

拖动手势(PanGesture)

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

拖动手势用于触发拖动手势事件,滑动达到最小滑动距离(默认值为5vp)时拖动手势识别成功,拥有三个可选参数:

  • fingers:非必选参数,用于声明触发拖动手势所需要的最少手指数量,最小值为1,最大值为10,默认值为1。
  • direction:非必选参数,用于声明触发拖动的手势方向,此枚举值支持逻辑与(&)和逻辑或(|)运算。默认值为Pandirection.All。
  • distance:非必选参数,用于声明触发拖动的最小拖动识别距离,单位为vp,默认值为5。

以在Text组件上绑定拖动手势为例,可以通过在拖动手势的回调函数中修改组件的布局位置信息来实现组件的拖动:

  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. // 绑定拖动手势
  21. PanGesture()
  22. .onActionStart((event: GestureEvent) => {
  23. console.info('Pan start');
  24. })
  25. // 当触发拖动手势时,根据回调函数修改组件的布局位置信息
  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. }

说明

大部分可滑动组件,如List、Grid、Scroll、Tab等组件是通过PanGesture实现滑动,在组件内部的子组件绑定拖动手势(PanGesture)或者滑动手势(SwipeGesture)会导致手势竞争。

当在子组件绑定PanGesture时,在子组件区域进行滑动仅触发子组件的PanGesture。如果需要父组件响应,需要通过修改手势绑定方法或者子组件向父组件传递消息进行实现,或者通过修改父子组件的PanGesture参数distance使得拖动更灵敏。当子组件绑定SwipeGesture时,由于PanGesture和SwipeGesture触发条件不同,需要修改PanGesture和SwipeGesture的参数以达到所需效果。

捏合手势(PinchGesture)

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

捏合手势用于触发捏合手势事件,触发捏合手势的最少手指数量为2指,最大为5指,最小识别距离为5vp,拥有两个可选参数:

  • fingers:非必选参数,用于声明触发捏合手势所需要的最少手指数量,最小值为2,最大值为5,默认值为2。
  • distance:非必选参数,用于声明触发捏合手势的最小距离,单位为vp,默认值为5。

以在Column组件上绑定三指捏合手势为例,可以通过在捏合手势的函数回调中获取缩放比例,实现对组件的缩小或放大:

  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. // 在组件上绑定缩放比例,可以通过修改缩放比例来实现组件的缩小或者放大
  20. .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
  21. .gesture(
  22. // 在组件上绑定三指触发的捏合手势
  23. PinchGesture({ fingers: 3 })
  24. .onActionStart((event: GestureEvent) => {
  25. console.info('Pinch start');
  26. })
  27. // 当捏合手势触发时,可以通过回调函数获取缩放比例,从而修改组件的缩放比例
  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. }

旋转手势(RotationGesture)

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

旋转手势用于触发旋转手势事件,触发旋转手势的最少手指数量为2指,最大为5指,最小改变度数为1度,拥有两个可选参数:

  • fingers:非必选参数,用于声明触发旋转手势所需要的最少手指数量,最小值为2,最大值为5,默认值为2。
  • angle:非必选参数,用于声明触发旋转手势的最小改变度数,单位为deg,默认值为1。

以在Text组件上绑定旋转手势实现组件的旋转为例,可以通过在旋转手势的回调函数中获取旋转角度,从而实现组件的旋转:

  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. // 在组件上绑定旋转布局,可以通过修改旋转角度来实现组件的旋转
  11. .rotate({ angle: this.angle })
  12. .gesture(
  13. RotationGesture()
  14. .onActionStart((event: GestureEvent) => {
  15. console.info('RotationGesture is onActionStart');
  16. })
  17. // 当旋转手势生效时,通过旋转手势的回调函数获取旋转角度,从而修改组件的旋转角度
  18. .onActionUpdate((event: GestureEvent) => {
  19. this.angle = this.rotateValue + event.angle;
  20. console.info('RotationGesture is onActionEnd');
  21. })
  22. // 当旋转结束抬手时,固定组件在旋转结束时的角度
  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. }

滑动手势(SwipeGesture)

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

滑动手势用于触发滑动事件,当滑动速度大于100vp/s时可以识别成功,拥有三个可选参数:

  • fingers:非必选参数,用于声明触发滑动手势所需要的最少手指数量,最小值为1,最大值为10,默认值为1。
  • direction:非必选参数,用于声明触发滑动手势的方向,此枚举值支持逻辑与(&)和逻辑或(|)运算。默认值为SwipeDirection.All。
  • speed:非必选参数,用于声明触发滑动的最小滑动识别速度,单位为vp/s,默认值为100。

以在Column组件上绑定滑动手势实现组件的旋转为例:

  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组件上绑定旋转,通过滑动手势的滑动速度和角度修改旋转的角度
  18. .rotate({ angle: this.rotateAngle })
  19. .gesture(
  20. // 绑定滑动手势且限制仅在竖直方向滑动时触发
  21. SwipeGesture({ direction: SwipeDirection.Vertical })
  22. // 当滑动手势触发时,获取滑动的速度和角度,实现对组件的布局参数的修改
  23. .onAction((event: GestureEvent) => {
  24. this.speed = event.speed;
  25. this.rotateAngle = event.angle;
  26. })
  27. )
  28. }
  29. }
  30. }

说明

当SwipeGesture和PanGesture同时绑定时,若二者是以默认方式或者互斥方式进行绑定时,会发生竞争。SwipeGesture的触发条件为滑动速度达到100vp/s,PanGesture的触发条件为滑动距离达到5vp,先达到触发条件的手势触发。可以通过修改SwipeGesture和PanGesture的参数以达到不同的效果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号