温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!
本文将详细介绍TabsConcaveCircle组件中的动画系统实现,包括选项切换动画和凹陷圆球的移动动画。
getAnimateSelectIndex() {
let animateDelay = 500;
animateTo({
duration: this.animateTime,
delay: animateDelay
}, () => {
this.animateSelectIndex = this.selectIndex
})
this.createAnimation()
}
animateDelay
: 设置500ms的延迟,用于等待上一个选项动画结束animateTo
: ArkUI提供的动画API,用于创建过渡动画duration
: 动画持续时间delay
: 动画延迟时间animateSelectIndex
,触发UI更新createAnimation()
开始圆球移动动画createAnimation() {
if (!this.circleInfo) {
return;
}
this.canvasAnimator = animator.create({
duration: this.animateTime,
easing: "ease",
delay: 0,
fill: "forwards",
direction: "normal",
iterations: 1,
begin: this.animationPositionX,
end: this.circleInfo?.getMenuCenterX(this.selectIndex)
})
this.canvasAnimator.onFrame = (value: number) => {
this.animationPositionX = value;
this.circleInfo?.setPositionXY({ x: value - this.circleInfo.circleRadius })
this.createCanvas()
}
this.canvasAnimator.play()
}
duration
: 动画持续时间easing
: 使用"ease"缓动函数fill
: "forwards"保持动画最后一帧状态direction
: "normal"表示正向播放iterations
: 1表示只播放一次begin
: 起始位置(当前位置)end
: 目标位置(选中项的中心位置)@Builder
TabItem(item: TabMenusInterfaceIRequired, index: number) {
Column() {
if (item.image) {
Image(getImageUrl(item as TabMenusInterfaceIRequired, index, this.selectIndex))
.size({
width: this.imageWH,
height: this.imageWH,
})
.interpolation(ImageInterpolation.High)
.offset({
y: this.selectIndex === index && this.animateSelectIndex === index ?
-this.imageOffsetY : 0,
})
.id(`${this.concaveCircleId}${index}`)
}
// ... 其他代码
}
}
imageWH
控制图片尺寸interpolation(ImageInterpolation.High)
offset
属性实现垂直方向的偏移imageOffsetY
控制aboutToAppear(): void {
this.listener = inspector.createComponentObserver(`${this.concaveCircleId}0`)
this.getImageOffsetY()
this.animateSelectIndex = this.selectIndex;
}
getImageOffsetY() {
let onLayoutComplete: () => void = (): void => {
let modePosition = componentUtils.getRectangleById(`${this.concaveCircleId}0`)
if (modePosition.localOffset) {
let halfHeight = px2vp(modePosition.size.height) / 2;
this.imageOffsetY = px2vp(modePosition.localOffset.y) + halfHeight;
this.listener?.off('draw')
}
}
let FuncDraw = onLayoutComplete;
this.listener?.on('draw', FuncDraw)
}
TabsConcaveCircle组件的动画系统主要包含三个部分:
这些动画协同工作,创造出流畅的用户交互体验。通过合理的动画配置和状态管理,实现了一个专业的底部导航栏组件。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。