在 OpenHarmony(ArkUI) 中,列表组件(如 List、LazyForEach、ForEach)的“动画”通常分为几类:列表项增删改动画、滚动动画、状态变化动画、转场动画。下面按常见场景给你说明。
ForEach + 状态变化(自动动画)OpenHarmony 的 ForEach 在数据源变化时会自动做进场 / 退场动画。
@Entry
@Component
struct ListAnimDemo {
@State items: string[] = ['A', 'B', 'C']
build() {
Column() {
Button('添加')
.onClick(() => {
this.items.push(`Item ${this.items.length}`)
})
Button('删除')
.onClick(() => {
this.items.pop()
})
List() {
ForEach(this.items, (item: string) => {
ListItem() {
Text(item)
.fontSize(20)
.padding(10)
}
}, (item: string) => item)
}
}
}
}
✅ 效果:
animateTo@State show: boolean = true
ListItem() {
Text('动画项')
.opacity(this.show ? 1 : 0)
.height(this.show ? 50 : 0)
}
Button('切换')
.onClick(() => {
animateTo({ duration: 300 }, () => {
this.show = !this.show
})
})
✅ 常用于:
List 滚动到指定位置private listScroller: ListScroller = new ListScroller()
List(this.listScroller) {
// items
}
Button('滚动到顶部')
.onClick(() => {
this.listScroller.scrollToIndex(0)
})
可选参数:
scrollToIndex(index, smooth: true)
✅ 支持平滑滚动动画
scale / translate 动画@State scaleValue: number = 1
ListItem() {
Text('点击放大')
.scale({ x: this.scaleValue, y: this.scaleValue })
.onClick(() => {
animateTo({ duration: 200 }, () => {
this.scaleValue = 1.2
})
})
}
pageTransitionpageTransition() {
PageTransitionEnter({ duration: 300, type: RouteType.Push })
PageTransitionExit({ duration: 300, type: RouteType.Pop })
}
适用于:
✅ 推荐
LazyForEach(长列表)opacity / translate / scale❌ 避免
width / height 动画setState| 场景 | 推荐方式 |
|---|---|
| 列表增删 | ForEach |
| 折叠展开 | animateTo |
| 滚动定位 | ListScroller |
| 点击反馈 | scale / opacity |
| 页面切换 | pageTransition |
如果你能告诉我:
我可以给你更精准的示例代码。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。