手势模块—-手势与动画结合:HarmonyOS6 PC打造极致交互体验 - this

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力网 👁️ 496 次阅读

文章目录

TapGesture:最基础也最重要单击与动画反馈双击识别

LongPressGesture:长按的"蓄力"反馈长按放大变色长按弹出菜单

PanGesture:拖拽动画的灵魂基础拖拽拖拽回弹

PinchGesture:缩放手势缩放配合双击重置

RotationGesture:旋转手势旋转 + 缩放组合

手势组合:ParallelGesture 和 ExclusiveGesture并行手势互斥手势

实战案例:可拖拽排序的卡片列表给手势交互加分的几个细节

我一直觉得,判断一个桌面应用做的好不好,有个很简单的标准:随便点几个按钮、拖几个元素,如果感觉"跟手"、“舒服”,那交互设计就是到位的。这种"到位"的感觉,背后是手势系统和动画系统的精密配合。

HarmonyOS6 PC 提供了一套相当完整的手势识别API,从最简单的点击到多指旋转都覆盖了。但光有手势识别还不够——用户做了手势,界面要给出即时的、符合直觉的动画反馈,才能形成闭环。今天这篇文章,我把每种手势和动画的配合方式都演示一遍,帮你做出真正"跟手"的交互体验。

TapGesture:最基础也最重要

TapGesture 就是点击手势。你可能会问,不是有 .onClick() 吗,为什么还要用 TapGesture?区别在于 TapGesture 提供了更多控制——比如你可以设置连续点击次数来识别双击、三击。

单击与动画反馈

@State tapScale: number = 1

@State tapColor: string = '#4D96FF'

Column()

.width(100).height(100)

.backgroundColor(this.tapColor)

.borderRadius(16)

.scale({ x: this.tapScale, y: this.tapScale })

.gesture(

TapGesture({ count: 1 })

.onAction(() => {

// 按下瞬间:缩小

animateTo({ duration: 80 }, () => {

this.tapScale = 0.92

})

// 弹回

setTimeout(() => {

animateTo({ duration: 250, curve: Curve.FastOutSlowIn }, () => {

this.tapScale = 1

})

}, 80)

})

)

这里的动画分两步:先用80毫秒缩小到92%,再用250毫秒弹回100%。两步的时长加起来330毫秒,正好是一个"干脆不拖沓"的点击反馈节奏。FastOutSlowIn 曲线让弹回过程前快后慢,有一种"弹性回位"的感觉。

双击识别

@State doubleTapScale: number = 1

Column()

.width(100).height(100)

.backgroundColor('#FFD93D')

.borderRadius(16)

.scale({ x: this.doubleTapScale, y: this.doubleTapScale })

.gesture(

TapGesture({ count: 2 })

.onAction(() => {

// 双击放大查看

animateTo({ duration: 400, curve: Curve.FastOutSlowIn }, () => {

this.doubleTapScale = this.doubleTapScale === 1 ? 1.5 : 1

})

})

)

count: 2 表示需要连续点击两次才触发。这是图片查看器里"双击放大/缩小"的标准交互。在 HarmonyOS6 PC 端的大屏上,这种操作比找放大按钮方便得多。

LongPressGesture:长按的"蓄力"反馈

长按手势的精髓在于"时间感"。用户按住不放,系统在一段时间后触发——这段时间就是"蓄力"的过程。动画要配合这种蓄力感,不能太轻飘。

长按放大变色

@State longPressScale: number = 1

@State longPressColor: string = '#4ECDC4'

@State longPressShadow: number = 0

Column()

.width(100).height(100)

.backgroundColor(this.longPressColor)

.borderRadius(16)

.scale({ x: this.longPressScale, y: this.longPressScale })

.shadow({ radius: this.longPressShadow, color: '#33000000', offsetY: 4 })

.gesture(

LongPressGesture({ repeat: false })

.onAction(() => {

animateTo({ duration: 400, curve: Curve.EaseInOut }, () => {

this.longPressScale = 1.2

this.longPressColor = '#FF6B6B'

this.longPressShadow = 12

})

})

.onActionEnd(() => {

animateTo({ duration: 500, curve: Curve.FastOutSlowIn }, () => {

this.longPressScale = 1

this.longPressColor = '#4ECDC4'

this.longPressShadow = 0

})

})

)

这里用了 onActionEnd 回调——当用户松开手指时触发。整个交互分两个阶段:按住时放大变色加阴影(像是一个气球在充气),松开时恢复原状(气球放气)。

注意 onAction 和 onActionEnd 用了不同的曲线和时长。触发时用 EaseInOut,400毫秒,比较稳重;恢复时用 FastOutSlowIn,500毫秒,更有弹性。这种"触发慢恢复快"的不对称设计,让长按操作有明确的"开始-结束"节奏感。

长按弹出菜单

长按的另一个经典用途是弹出上下文菜单:

@State menuScale: number = 0

@State menuOpacity: number = 0

// 长按目标

Column()

.width(80).height(80)

.backgroundColor('#6BCB77').borderRadius(12)

.gesture(

LongPressGesture()

.onAction(() => {

animateTo({ duration: 250, curve: Curve.FastOutSlowIn }, () => {

this.menuScale = 1

this.menuOpacity = 1

})

})

)

// 弹出菜单

Column() {

Text('复制').fontSize(14)

Text('删除').fontSize(14)

Text('分享').fontSize(14)

}

.scale({ x: this.menuScale, y: this.menuScale })

.opacity(this.menuOpacity)

菜单从0缩放到1,同时从透明到不透明,250毫秒完成。FastOutSlowIn 曲线让菜单"弹出来"而不是"长出来",给用户一种"菜单一直在,只是被叫出来了"的感觉。

PanGesture:拖拽动画的灵魂

PanGesture 是平移手势,也就是拖拽。这是和动画结合最紧密的手势——因为拖拽过程中元素要"跟手"移动,松手后还要有"惯性"或"回弹"动画。

基础拖拽

@State dragX: number = 0

@State dragY: number = 0

@State isDragging: boolean = false

@State lastX: number = 0

@State lastY: number = 0

Column()

.width(80).height(80)

.backgroundColor('#9B59B6')

.borderRadius(12)

.translate({ x: this.dragX, y: this.dragY })

.scale({

x: this.isDragging ? 1.05 : 1,

y: this.isDragging ? 1.05 : 1

})

.shadow({

radius: this.isDragging ? 12 : 2,

color: '#33000000',

offsetY: this.isDragging ? 6 : 2

})

.animation({ duration: 150, curve: Curve.EaseOut })

.gesture(

PanGesture()

.onActionStart(() => {

this.isDragging = true

})

.onActionUpdate((event: GestureEvent) => {

this.dragX = this.lastX + event.offsetX

this.dragY = this.lastY + event.offsetY

})

.onActionEnd(() => {

this.isDragging = false

this.lastX = this.dragX

this.lastY = this.dragY

})

)

拖拽有三个阶段。onActionStart 标记开始拖拽,元素微微放大并抬高阴影——给用户一种"我把它拿起来了"的视觉反馈。onActionUpdate 在拖拽过程中持续触发,实时更新位置。onActionEnd 拖拽结束,元素缩回原始大小,阴影降低。

这里的 .animation({ duration: 150 }) 只作用于 scale 和 shadow 的变化(拿起和放下的反馈),不作用于 translate。因为 translate 在 onActionUpdate 里被高频更新,如果也带动画会"不跟手"。

拖拽回弹

很多场景下,元素只能拖到一定范围,超出范围后要弹回原位:

@State cardX: number = 0

@State cardY: number = 0

@State cardScale: number = 1

Column()

.width(120).height(80)

.backgroundColor('#4D96FF').borderRadius(12)

.translate({ x: this.cardX, y: this.cardY })

.scale({ x: this.cardScale, y: this.cardScale })

.gesture(

PanGesture()

.onActionStart(() => {

this.cardScale = 1.05

})

.onActionUpdate((event: GestureEvent) => {

this.cardX = event.offsetX

this.cardY = event.offsetY

})

.onActionEnd(() => {

// 检查是否超出范围

const distance = Math.sqrt(

this.cardX * this.cardX + this.cardY * this.cardY

)

if (distance < 100) {

// 没拖够距离,弹回原位

animateTo({ duration: 400, curve: Curve.FastOutSlowIn }, () => {

this.cardX = 0

this.cardY = 0

this.cardScale = 1

})

} else {

// 拖够了距离,执行操作(比如删除)

animateTo({ duration: 300, curve: Curve.EaseIn }, () => {

this.cardX = this.cardX > 0 ? 400 : -400

this.cardScale = 0.8

})

}

})

)

这个例子模拟了"拖拽删除"的交互:把卡片拖出去一定距离就飞走删除,没拖够就弹回原位。弹回用 FastOutSlowIn 曲线,像橡皮筋一样弹回去;飞走用 EaseIn 曲线,加速飞出视野。

PinchGesture:缩放手势

PinchGesture 是双指捏合手势,在 PC 端通常通过触控板或触摸屏触发。它是图片查看器、地图应用的核心交互。

@State pinchScale: number = 1

@State currentScale: number = 1

@State pinchRotate: number = 0

Stack() {

Image($r('app.media.sample_photo'))

.width(300).height(200)

.objectFit(ImageFit.Cover)

.borderRadius(12)

}

.scale({ x: this.pinchScale, y: this.pinchScale })

.gesture(

PinchGesture({ fingers: 2 })

.onActionStart(() => {

// 记录当前缩放值

})

.onActionUpdate((event: GestureEvent) => {

this.pinchScale = this.currentScale * event.scale

})

.onActionEnd(() => {

// 限制缩放范围

if (this.pinchScale < 0.5) {

animateTo({ duration: 300, curve: Curve.FastOutSlowIn }, () => {

this.pinchScale = 0.5

})

} else if (this.pinchScale > 3) {

animateTo({ duration: 300, curve: Curve.FastOutSlowIn }, () => {

this.pinchScale = 3

})

}

this.currentScale = this.pinchScale

})

)

在 onActionUpdate 中,event.scale 是捏合的比例因子。比如两指张开到原来的1.5倍,event.scale 就是1.5。我们把当前缩放值乘以这个因子,就得到了新的缩放值。

onActionEnd 里做了范围限制:最小0.5倍,最大3倍。如果用户缩得太小或太大,松手后自动弹回到边界值,用 FastOutSlowIn 曲线制造弹性回弹的效果。

缩放配合双击重置

.gesture(

PinchGesture({ fingers: 2 })

.onActionUpdate((event: GestureEvent) => { 拼多多帮助力入口

this.pinchScale = this.currentScale * event.scale

})

.onActionEnd(() => {

this.currentScale = this.pinchScale

})

)

.gesture(

TapGesture({ count: 2 })

.onAction(() => {

animateTo({ duration: 400, curve: Curve.FastOutSlowIn }, () => {

this.pinchScale = 1

this.currentScale = 1

})

})

)

一个元素可以同时挂多个手势。双指捏合用来缩放,双击用来重置为原始大小。两种手势互不冲突,用户体验非常流畅。

RotationGesture:旋转手势

RotationGesture 是双指旋转手势,在图片编辑、画板应用中很常见。

@State rotateAngle: number = 0

@State currentAngle: number = 0

Column()

.width(120).height(120)

.backgroundColor('#FF6B9D')

.borderRadius(16)

.rotate({ angle: this.rotateAngle })

.gesture(

RotationGesture()

.onActionStart(() => {

// 开始旋转

})

.onActionUpdate((event: GestureEvent) => {

this.rotateAngle = this.currentAngle + event.angle

})

.onActionEnd(() => {

this.currentAngle = this.rotateAngle

// 自动吸附到最近的90度

const nearest90 = Math.round(this.rotateAngle / 90) * 90

animateTo({ duration: 300, curve: Curve.EaseOut }, () => {

this.rotateAngle = nearest90

this.currentAngle = nearest90

})

})

)

旋转过程中实时更新角度,松手后自动吸附到最近的90度整数倍(0、90、180、270)。这种"吸附"效果在很多场景下都很实用——比如图片查看器里,用户旋转图片时通常希望它能精确地摆正。

吸附动画用 EaseOut 曲线,旋转最后一点角度时慢慢停下,很自然。

旋转 + 缩放组合

@State combinedRotate: number = 0

@State combinedScale: number = 1

@State baseRotate: number = 0

@State baseScale: number = 1

Column()

.width(150).height(100)

.backgroundColor('#4ECDC4')

.borderRadius(12)

.rotate({ angle: this.combinedRotate })

.scale({ x: this.combinedScale, y: this.combinedScale })

.gesture(

RotationGesture()

.onActionUpdate((event: GestureEvent) => {

this.combinedRotate = this.baseRotate + event.angle

this.combinedScale = this.baseScale * event.scale

})

.onActionEnd(() => {

this.baseRotate = this.combinedRotate

this.baseScale = this.combinedScale

})

)

RotationGesture 的 event 对象同时包含旋转角度和缩放比例(因为双指旋转时手指距离通常也会变化)。我们可以同时利用这两个值,让元素一边旋转一边缩放。这在地图应用中很常见——双指旋转地图时,地图也会随着手指距离变化而缩放。

手势组合:ParallelGesture 和 ExclusiveGesture

实际应用中,一个元素可能需要同时支持多种手势。HarmonyOS6 PC 提供了两种组合方式。

并行手势

Column()

.width(100).height(100)

.gesture(

PanGesture()

.onActionUpdate((event: GestureEvent) => {

this.dragX = event.offsetX

})

)

.parallelGesture(

TapGesture({ count: 2 })

.onAction(() => {

animateTo({ duration: 300 }, () => {

this.doubleTapScale = 1.5

})

})

)

.parallelGesture() 允许两种手势同时识别。拖拽的过程中也可以触发双击。这在复杂交互中很有用——比如一个可拖拽的地图,双击可以放大。

互斥手势

Column()

.width(100).height(100)

.gesture(

PanGesture({ direction: PanDirection.Horizontal })

.onActionUpdate((event: GestureEvent) => {

this.swipeX = event.offsetX

})

)

.priorityGesture(

PanGesture({ direction: PanDirection.Vertical })

.onActionUpdate((event: GestureEvent) => {

this.scrollY = event.offsetY

})

)

.priorityGesture() 让优先级高的手势先识别。如果用户垂直滑动,垂直拖拽先触发;如果水平滑动,水平拖拽触发。这种互斥关系避免了手势冲突。

实战案例:可拖拽排序的卡片列表

把所有手势和动画知识综合起来,我们做一个可拖拽排序的卡片列表:

@State cards: number[] = [1, 2, 3, 4, 5]

@State dragIndex: number = -1

@State dragOffsetY: number = 0

@State isDragging: boolean = false

build() {

Column() {

ForEach(this.cards, (card: number, index: number) => {

Row() {

Text(`卡片 ${card}`)

.fontSize(16)

}

.width('100%')

.height(60)

.backgroundColor(this.dragIndex === index ? '#E3F2FD' : '#FFFFFF')

.borderRadius(12)

.translate({

y: this.dragIndex === index ? this.dragOffsetY : 0

})

.scale({

x: this.dragIndex === index ? 1.03 : 1,

y: this.dragIndex === index ? 1.03 : 1

})

.shadow({

radius: this.dragIndex === index ? 10 : 1,

color: '#22000000',

offsetY: this.dragIndex === index ? 4 : 1

})

.animation({ duration: 200, curve: Curve.EaseOut })

.gesture(

LongPressGesture()

.onAction(() => {

this.dragIndex = index

this.isDragging = true

})

)

.gesture(

PanGesture({ direction: PanDirection.Vertical })

.onActionUpdate((event: GestureEvent) => {

if (this.dragIndex === index) {

this.dragOffsetY = event.offsetY

}

})

.onActionEnd(() => {

// 计算新位置并排序

const newIndex = Math.round(

(index + this.dragOffsetY / 68)

)

// 重置动画

animateTo({ duration: 300, curve: Curve.FastOutSlowIn }, () => {

this.dragOffsetY = 0

this.dragIndex = -1

this.isDragging = false

})

})

)

.margin({ bottom: 8 })

})

}

}

长按卡片进入拖拽状态(放大、抬高、变背景色),然后垂直拖拽移动位置,松手后弹回列表位置。整个交互过程中,动画配合手势提供了三重反馈:

视觉反馈:放大+阴影表示"拿起"位置反馈:translate 跟手移动归位反馈:松手后的弹回动画

给手势交互加分的几个细节

手势和动画的配合,细节决定成败。

拖拽开始时要有"拿起"的过渡。 不要一拖就动,先用100-150毫秒做一个轻微的放大+阴影效果,暗示用户"你已经拿起来了"。然后再跟手移动。

松手时要有"放下"的过渡。 拖拽中的元素通常是放大+高阴影的,松手后要缩回+降阴影。这个过渡用150-200毫秒就够,太快会突兀,太慢会拖沓。

手势失败时也要有反馈。 比如拖拽距离不够,需要弹回原位。弹回动画用 FastOutSlowIn 曲线,时长300-400毫秒,让用户清楚地看到"操作没有成功,元素回到了原来的位置"。

PC 端的鼠标事件也可以结合。 HarmonyOS6 PC 支持 onHover 和 onMouseDown 等鼠标事件。在拖拽的基础上,加上 hover 时的微微上浮效果,整个交互会更丰富。

手势是用户和界面之间的"对话",动画是界面的"回应"。只有两者完美配合,用户才能感受到"界面在听我说话"。HarmonyOS6 PC 提供了足够强大的手势和动画工具,能不能用好,就看你的功力了。

代码都在这儿了,打开 DevEco Studio 试一遍,拖一拖、捏一捏、转一转,手感这种东西,只有亲手试过才知道。,手势模块

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧