首页 > 其他分享 >OpenHarmony/HarmonyOS开发中的基础手势之PanGesture

OpenHarmony/HarmonyOS开发中的基础手势之PanGesture

时间:2023-06-18 14:04:49浏览次数:40  
标签:OpenHarmony 触发 拖动 number HarmonyOS PanGesture 手势 event Pan

PanGesture

作者:坚果
团队:坚果派
公众号:“大前端之旅”
润开鸿技术专家,华为HDE,InfoQ签约作者,OpenHarmony布道师,擅长HarmonyOS应用开发、熟悉服务卡片开发,在“战码先锋”活动中作为大队长,累计培养三个小队长,带领100+队员完成Pr的提交合入。
欢迎通过主页或者私信联系我,加入坚果派,一起学习OpenHarmony/HarmonyO应用开发。

今天我们一起来看一下基础手势的拖动手势。用于触发拖动手势事件,滑动的最小距离为5vp时拖动手势识别成功。

适用

语言:ArkTS

版本:API9

OpenHarmony:3.2Release

HarmonyOS:3.1

接口

interface PanGestureInterface {
    /**
     * Set the value.
     * @since 7
     */
    (value?: {
        fingers?: number;
        direction?: PanDirection;
        distance?: number;
    } | PanGestureOptions): PanGestureInterface;
    /**
     * Pan gesture recognition success callback.
     * @since 7
     */
    onActionStart(event: (event?: GestureEvent) => void): PanGestureInterface;
    /**
     * Callback when the Pan gesture is moving.
     * @since 7
     */
    onActionUpdate(event: (event?: GestureEvent) => void): PanGestureInterface;
    /**
     * The Pan gesture is successfully recognized. When the finger is lifted, the callback is triggered.
     * @since 7
     */
    onActionEnd(event: (event?: GestureEvent) => void): PanGestureInterface;
    /**
     * The Pan gesture is successfully recognized and a callback is triggered when the touch cancel event is received.
     * @since 7
     */
    onActionCancel(event: () => void): PanGestureInterface;
}

接下来对以上几个参数进行说明。

  • fingers:触发拖动的最少手指数,最小为1指, 最大取值为10指。默认值:1取值范围:[1,10]说明: 当设置的值小于1或不设置时,会被转化为默认值。
  • direction:触发拖动的手势方向,此枚举值支持逻辑与(&)和逻辑或(|)运算。默认值:PanDirection.All
  • distance:最小拖动识别距离,单位为vp。默认值:5说明: Tabs组件滑动与该拖动手势事件同时存在时,可将distance值设为1,使拖动更灵敏,避免造成事件错乱。

以上就是几个参数的说明

事件

  • onActionStart(event: (event?: GestureEvent) => void):Pan手势识别成功回调。
  • onActionUpdate(event: (event?: GestureEvent) => void):Pan手势移动过程中回调。
  • onActionEnd(event: (event?: GestureEvent) => void):Pan手势识别成功,手指抬起后触发回调。
  • onActionCancel(event: () => void):Pan手势识别成功,接收到触摸取消事件触发回调。

这里我们继续对后面的几个值进行学习。

PanGestureOptions

declare class PanGestureOptions {
    /**
     * Constructor parameters.
     * @since 7
     */
    constructor(value?: {
        fingers?: number;
        direction?: PanDirection;
        distance?: number;
    });
    /**
     * Sets the direction attribute.
     * @since 7
     */
    setDirection(value: PanDirection);
    /**
     * Sets the setDistance attribute.
     * @since 7
     */
    setDistance(value: number);
    /**
     * Sets the setFingers attribute.
     * @since 7
     */
    setFingers(value: number);
}

通过PanGestureOptions对象接口可以动态修改滑动手势识别器的属性,从而避免通过状态变量修改属性(状态变量修改会导致UI刷新)。

  • fingers:触发拖动的最少手指数,最小为1指, 最大取值为10指。默认值:1
  • direction:触发拖动的手势方向,此枚举值支持逻辑与(&)和逻辑或(|)运算。默认值:PanDirection.All
  • distance:最小拖动识别距离,单位为vp。默认值:5说明:Tabs组件滑动与该拖动手势事件同时存在时,可将distance值设为1,使拖动更灵敏,避免造成事件错乱。

接口

  • setDirection(value: PanDirection):设置direction属性。
  • setDistance(value: number):设置distance属性。
  • setFingers(value: number):设置fingers属性。

最后我们对上面所用到的枚举值进行学习。

anDirection枚举说明

  • All:所有方向。
  • Horizontal:水平方向。
  • Vertical:竖直方向。
  • Left:向左拖动。
  • Right:向右拖动。
  • Up:向上拖动。
  • Down:向下拖动。
  • None:任何方向都不可触发拖动手势事件。

以上就是关于拖动手势所有的学习。

最后给大家附上完整的源码。

// xxx.ets
@Entry
@Component
struct PanGesturePage{
  @State offsetX: number = 0
  @State offsetY: number = 0
  @State positionX: number = 0
  @State positionY: number = 0
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Right })

  build() {
    Column() {
      Column() {
        Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
      }
      .height(200)
      .width(300)
      .padding(20)
      .border({ width: 3 })
      .margin(50)
      .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
      // 左右拖动触发该手势事件
      .gesture(
        PanGesture(this.panOption)
          .onActionStart((event: GestureEvent) => {
           // Pan手势识别成功回调。
            console.info('Pan start')
          })
          .onActionUpdate((event: GestureEvent) => {
            //Pan手势移动过程中回调。
            this.offsetX = this.positionX + event.offsetX
            this.offsetY = this.positionY + event.offsetY
          })
          .onActionEnd(() => {
            //Pan手势识别成功,手指抬起后触发回调。
            this.positionX = this.offsetX
            this.positionY = this.offsetY
            console.info('Pan end')
          }).onActionCancel(()=>{

          //Pan手势识别成功,接收到触摸取消事件触发回调。
          // Pan手势识别成功回调。
          console.info('Pan end')
        })
      )

      Button('修改PanGesture触发条件')
        .onClick(() => {
          // 将PanGesture手势事件触发条件改为双指以任意方向拖动
          //设置direction属性。
          this.panOption.setDirection(PanDirection.All)
          this.panOption.setFingers(2)
        })
    }
  }
}

标签:OpenHarmony,触发,拖动,number,HarmonyOS,PanGesture,手势,event,Pan
From: https://blog.51cto.com/jianguo/6508339

相关文章

  • HarmonyOS应用开发者基础认证题库
    祝大家都顺利通过判断题1.Web组件对于所有的网页都可以使用zoom(factor:number)方法进行缩放。错误(False)2.每一个自定义组件都有自己的生命周期正确(True)3.每调用一次router.pushUrl()方法,默认情况下,页面栈数量会加1,页面栈支持的最大页面数量为32。正确(True)4.所......
  • HarmonyOS 极客马拉松2023 正式启动,诚邀极客们用键盘码出无限可能!
     原文:https://mp.weixin.qq.com/s/p2yIs0rMmDE2BwhzsAtr7A,点击链接查看更多技术内容。 2023年6月15日, HarmonyOS极客马拉松2023开赛!期待各位开发者极客朋友一起,探索移动应用和服务的更多可能性!  HarmonyOS 极客松 2023 为你的“异想天开”保驾护航本次极客松采用......
  • OpenHarmony 如何切换输入法
    1、命令切换:默认已配置环境,hdc可用hdc_std.exeshellaastartability-aInputMethod-bcn.openharmony.inputmethodchoosedialog 2、代码切换:importinputMethodfrom'@ohos.inputmethod'...letsetting=inputMethod.getInputMethodSetting()setting.displayOptional......
  • HarmonyOS在SDK9版本下FA模型geolocation无法定位问题解决
    问题描述已经在config.json中加入了ohos.permission.LOCATION权限声明,但是在实际开发中,我使用geolocation.getCurrentLocation().then((result)=>{this.locationInfo=JSON.stringify(result);this.blog.setTitle(this.locationInfo);});获取位置信息得不到结果我使用的......
  • 开源共建下一代智能终端操作系统根社区 OpenHarmony携手伙伴聚力前行
    6月12日,2023开放原子全球开源峰会OpenHarmony分论坛(以下简称“分论坛”)在北京成功召开。OpenHarmony共建单位、生态伙伴齐聚一堂,共同展现了OpenHarmony在千行百业的落地成果、繁荣生态与最新技术进展。OpenHarmony项目群工作委员会委员、华为终端BG软件部副总裁柳晓见受邀出席......
  • 1加6T初步适配OpenHarmony-4.0-Beta1体验包更新,及相关仓开源
    1加6T初步适配OpenHarmony-4.0-Beta1体验包更新,及相关仓开源OpenHarmony官方于上周发布了OpenHarmony-4.0-Beta1版本,版本详细介绍:https://gitee.com/openharmony/docs/blob/master/zh-cn/release-notes/OpenHarmony-v4.0-beta1.md。相对于OpenHarmony-3.2的适配,4.0-Beta1还是有......
  • OpenHarmony 3.2 Release新特性解读之驱动HCS
    OpenAtomOpenHarmony(以下简称“OpenHarmony”)开源社区,在今年4月正式发布了OpenHarmony3.2Release版本,标准系统能力进一步完善,提升了系统的整体性能、稳定性和安全性。此次版本对于驱动也提供了一些新的特性,极大的方便了驱动开发工作。其中针对HCS文件开发提供新的开发思路,本文就......
  • OpenHarmony 4.0 Beta1发布,邀您体验
     初夏之际,OpenAtom OpenHarmony(简称“OpenHarmony”) 4.0 Beta1版本如期而至。4.0 Beta1版本在3.2 Release版本基础上,继续提升标准系统的ArkUI、应用框架、图形媒体等子系统能力,并提供首批API Level 10接口。作为OpenHarmony 4.0的首个Beta版本,推出了系列新特性。......
  • OpenHarmony系统之Service代码一键生成工具介绍
    作者:苟晶晶前言当开发者为OpenHarmony系统框架开发某些功能时,有时需要将这个功能包装成一个独立的服务进程运行在系统中,为了其它应用进程能够调用此服务,开发人员需要基于系统IPC通信框架编写一套远程接口调用实现。实现Service远程调用接口需要开发人员熟悉IPC通信框架,了解proxy/......
  • 【HarmonyOS】【ArkTS】如何使用HTTP网络请求获取动态数据刷新UI界面
    【关键字】HttpRequest、ArkTS、网络数据请求、@ohos.net.http【前言】在使用ArkTS开发HarmonyOS应用时,需要调用HTTP网络请求 @ohos.net.http 动态获取数据,进行UI列表刷新,这想必是应用开发最常见的功能。但是根据官网网络请求的示例代码进行功能开发时,封装方法进行HTTP请求后,返回......