鸿蒙手势交互(二:单一手势)

news/2024/9/22 15:22:03/

在这里插入图片描述

二、单一手势

有六种:点击手势(TapGesture)、长按手势(LongPressGesture)、拖动手势(PanGesture)
捏合手势(PinchGesture)、旋转手势(RotationGesture)、滑动手势(SwipeGesture)

  1. 点击手势(TapGesture)
TapGesture(value?:{count?:number, fingers?:number})
//- count:声明该点击手势识别的连续点击次数。默认值为1,若设置小于1的非法值会被转化为默认//值。如果配置多次点击,上一次抬起和下一次按下的超时时间为300毫秒。//- fingers:用于声明触发点击的手指数量,最小值为1,最大值为10,默认值为1。当配置多指时,若第一根手指按下300毫秒内未有足够的手指数按下则手势识别失败。// 绑定count为2的TapGesture
@Entry
@Component
struct Index {@State value: string = "";build() {Column() {Text('Click twice').fontSize(28).gesture(// 点击两次触发回调TapGesture({ count: 2 }).onAction((event: GestureEvent|undefined) => {if(event){this.value = JSON.stringify(event.fingerList[0]);}}))Text(this.value)}.height(200).width(250).padding(20).border({ width: 3 }).margin(30)}
}
  1. 长按手势(LongPressGesture)
LongPressGesture(value?:{fingers?:number, repeat?:boolean, duration?:number})//- fingers:用于声明触发长按手势所需要的最少手指数量,最小值为1,最大值为10,默认值为1。//- repeat:用于声明是否连续触发事件回调,默认值为false。//- duration:用于声明触发长按所需的最短时间,单位为毫秒,默认值为500。// 以在Text组件上绑定可以重复触发的长按手势为例
@Entry
@Component
struct Index {@State count: number = 0;build() {Column() {Text('LongPress OnAction:' + this.count).fontSize(28).gesture(// 一直按住,每隔500ms则一直触发LongPressGesture({ repeat: true }).onAction((event: GestureEvent|undefined) => {if(event){if (event.repeat) {this.count++;}}}).onActionEnd(() => {this.count = 0;}))}.height(200).width(250).padding(20).border({ width: 3 }).margin(30)}
}
  1. 拖动手势(PanGesture)
PanGesture(value?:{ fingers?:number, direction?:PanDirection, distance?:number})
//- fingers:用于声明触发拖动手势所需要的最少手指数量,最小值为1,最大值为10,默认值为1。//- direction:用于声明触发拖动的手势方向,此枚举值支持逻辑与(&)和逻辑或(|)运算。默认值为Pandirection.All。//- distance:用于声明触发拖动的最小拖动识别距离,单位为vp,默认值为5。// 拖动Text文本可实时更新的一个例子
@Entry
@Component
struct Index {@State offsetX: number = 0;@State offsetY: number = 0;@State positionX: number = 0;@State positionY: number = 0;build() {Column() {Text('PanGesture Offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY).fontSize(28).height(200).width(300).padding(20).border({ width: 3 })// 在组件上绑定布局位置信息.translate({ x: this.offsetX, y: this.offsetY, z: 0 }).gesture(// 绑定拖动手势PanGesture().onActionStart((event: GestureEvent|undefined) => {console.info('Pan start');})// 当触发拖动手势时,根据回调函数修改组件的布局位置信息.onActionUpdate((event: GestureEvent|undefined) => {if(event){this.offsetX = this.positionX + event.offsetX;this.offsetY = this.positionY + event.offsetY;}}).onActionEnd(() => {this.positionX = this.offsetX;this.positionY = this.offsetY;}))}.height(200).width(250)}
}
  1. 捏合手势(PinchGesture)
PinchGesture(value?:{fingers?:number, distance?:number})
// - fingers:用于声明触发捏合手势所需要的最少手指数量,最小值为2,最大值为5,默认值为2。// - distance:用于声明触发捏合手势的最小距离,单位为vp,默认值为5。// 手指捏合则实时更新文本框
@Entry
@Component
struct Index {@State scaleValue: number = 1;@State pinchValue: number = 1;@State pinchX: number = 0;@State pinchY: number = 0;build() {Column() {Column() {Text('PinchGesture scale:\n' + this.scaleValue)Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')}.height(200).width(300).border({ width: 3 }).margin({ top: 100 })// 在组件上绑定缩放比例,可以通过修改缩放比例来实现组件的缩小或者放大.scale({ x: this.scaleValue, y: this.scaleValue, z: 1 }).gesture(// 在组件上绑定三指触发的捏合手势PinchGesture({ fingers: 3 }).onActionStart((event: GestureEvent|undefined) => {console.info('Pinch start');})// 当捏合手势触发时,可以通过回调函数获取缩放比例,从而修改组件的缩放比例.onActionUpdate((event: GestureEvent|undefined) => {if(event){this.scaleValue = this.pinchValue * event.scale;this.pinchX = event.pinchCenterX;this.pinchY = event.pinchCenterY;}}).onActionEnd(() => {this.pinchValue = this.scaleValue;console.info('Pinch end');}))}}
}
  1. 旋转手势(RotationGesture)
RotationGesture(value?:{fingers?:number, angle?:number})
//- fingers:用于声明触发旋转手势所需要的最少手指数量,最小值为2,最大值为5,默认值为2。//- angle:用于声明触发旋转手势的最小改变度数,单位为deg,默认值为1。// 实时旋转文本框
@Entry
@Component
struct Index {@State angle: number = 0;@State rotateValue: number = 0;build() {Column() {Text('RotationGesture angle:' + this.angle).fontSize(28)// 在组件上绑定旋转布局,可以通过修改旋转角度来实现组件的旋转.rotate({ angle: this.angle }).gesture(RotationGesture().onActionStart((event: GestureEvent|undefined) => {console.info('RotationGesture is onActionStart');})// 当旋转手势生效时,通过旋转手势的回调函数获取旋转角度,从而修改组件的旋转角度.onActionUpdate((event: GestureEvent|undefined) => {if(event){this.angle = this.rotateValue + event.angle;}console.info('RotationGesture is onActionEnd');})// 当旋转结束抬手时,固定组件在旋转结束时的角度.onActionEnd(() => {this.rotateValue = this.angle;console.info('RotationGesture is onActionEnd');}).onActionCancel(() => {console.info('RotationGesture is onActionCancel');})).height(200).width(300).padding(20).border({ width: 3 }).margin(100)}}
}
  1. 滑动手势(SwipeGesture)
SwipeGesture(value?:{fingers?:number, direction?:SwipeDirection, speed?:number})
//- fingers:用于声明触发滑动手势所需要的最少手指数量,最小值为1,最大值为10,默认值为1。//- direction:用于声明触发滑动手势的方向,此枚举值支持逻辑与(&)和逻辑或(|)运算。默认值为SwipeDirection.All。//- speed:用于声明触发滑动的最小滑动识别速度,单位为vp/s,默认值为100。// 在Column组件上绑定滑动手势实现组件的旋转
@Entry
@Component
struct Index {@State rotateAngle: number = 0;@State speed: number = 1;build() {Column() {Column() {Text("SwipeGesture speed\n" + this.speed)Text("SwipeGesture angle\n" + this.rotateAngle)}.border({ width: 3 }).width(300).height(200).margin(100)// 在Column组件上绑定旋转,通过滑动手势的滑动速度和角度修改旋转的角度.rotate({ angle: this.rotateAngle }).gesture(// 绑定滑动手势且限制仅在竖直方向滑动时触发SwipeGesture({ direction: SwipeDirection.Vertical })// 当滑动手势触发时,获取滑动的速度和角度,实现对组件的布局参数的修改.onAction((event: GestureEvent|undefined) => {if(event){this.speed = event.speed;this.rotateAngle = event.angle;}}))}}
}

http://www.ppmy.cn/news/1528874.html

相关文章

LNMP环境搭建、部署的整体流程和详细步骤

LNMP(Linux Nginx MySQL PHP)环境是在Linux操作系统上构建的一个高性能Web服务器环境。这种组合因其高并发处理能力和灵活性而受到许多开发者的青睐。下面是搭建LNMP环境的整体流程和详细步骤: 1. 准备Linux系统 首先确保你已经在一台服…

GPU使用

0. 写这篇文章的背景 最近还是在使用GPU、连接远程服务器上出现了一点问题,发现在这方面的知识还是学得很模糊。(最让人感到困惑的是之前GPU的使用都没有问题) 总结一下最近的问题: 1.每一次连接远程服务器(选择的Ubuntu22.04),使用服务器的文件夹还好(关键是现在用…

【busybox记录】【shell指令】sleep

目录 内容来源: 【GUN】【sleep】指令介绍 【busybox】【sleep】指令介绍 【linux】【sleep】指令介绍 使用示例: 1秒、234毫秒、567微秒和890纳秒的睡眠: 常用组合指令: 指令不常用/组合用法还需继续挖掘: 内容来源&…

关于github GPG的配置

GitHub 使用 OpenPGP 库来确认本地签名的提交和标记,是否根据你在 GitHub.com 上添加到帐户的公钥进行加密验证。 这里是github关于GPG密钥的文档:https://docs.github.com/zh/authentication/managing-commit-signature-verification/about-commit-sig…

【Opencv知识】图像梯度如何理解?

在图像处理和计算机视觉中,理解和计算图像的梯度是非常重要的步骤。梯度图像表示的是图像在每个像素点上的亮度变化率,通常用于边缘检测和特征提取等任务。 灰度化是将彩色图像转换为灰度图像的过程,这样每个像素只包含一个亮度值&#xff0…

解决引入ethereumjs-wallet报错Uncaught ReferenceError: process is defined

最近在学习web3.0开发DApp的视频教程,遇到用助记词生成秘钥对报错Uncaught ReferenceError: process is defined。具体报错如下截图: 在网上搜了好几个小时都没找到解决方法。突然在学习的视频论坛找到有哥们说是引入的包不兼容问题导致报这个错。我试了…

Video.js展示视频

在Vue 2中使用Video.js来展示视频是一个常见的需求,因为Video.js提供了丰富的API和灵活的自定义选项,使得在网页上嵌入和控制视频变得简单。以下是一个详细的示例,包括如何在Vue 2项目中集成Video.js,并展示一个基本的视频播放器。…

手写流程图元素检测系统源码分享

手写流程图元素检测检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Comput…