HarmonyOS 鸿蒙Next中如何实现手风琴动画
HarmonyOS 鸿蒙Next中如何实现手风琴动画
鸿蒙如何实现手风琴动画
4 回复
@Entry
@Component
struct ShadesDemo {
@State widths: string[] = ['20%', '20%', '20%', '20%', '20%'] // 初始宽度数组
@State colors: number[] = [Color.Green, Color.Blue, Color.Yellow, Color.Red, Color.Orange]
build() {
Flex({ direction: FlexDirection.Row }) {
ForEach(this.widths, (width: string, index) => {
Column() {
Image('')
.width('100%')
.height('100%')
.draggable(false)
.backgroundColor(this.colors[index])
}
.width(this.widths[index])
.height(300)
.onTouch((event: TouchEvent) => {
// 手指离开组件时还原宽度
if (event.type === TouchType.Up) {
this.getUIContext().animateTo({ duration: 300, curve: Curve.Ease }, () => {
this.widths = ['20%', '20%', '20%', '20%', '20%']
})
} else {
// 手指触摸组件时触发宽度更新
this.updateWidths(index)
}
})
}, (width: string, index: number) => index.toString())
}
}
private updateWidths(activeIndex: number) {
this.getUIContext().animateTo({ duration: 300, curve: Curve.Ease }, () => {
this.widths = this.widths.map((_, idx) =>
idx === activeIndex ? '60%' : '10%'
)
})
}
}
更多关于HarmonyOS 鸿蒙Next中如何实现手风琴动画的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
可以使用显式动画animateTo结合条件渲染if控制 ListItem内容区域的展开和收起:
@Entry
@Component
struct ListCollapseExpand {
private arr: number[] = [0, 1, 2, 3, 4, 5, 6];
@State isContentShow: boolean = true;
@State selectItem: number = 0;
build() {
Column() {
List({ initialIndex: 0 }) {
ForEach(this.arr, (item: number, index: number) => {
ListItem() {
Column() {
Text(item.toString())
.height(40)
.width('100%')
.onClick(() => {
this.getUIContext().animateTo({
duration: 400,
onFinish: () => {
console.info('animation end');
}
}, () => {
this.isContentShow = !this.isContentShow;
this.selectItem = item;
})
})
if (this.selectItem === item) {
Text('这是内容区域')
.backgroundColor(Color.Gray)
.width('100%')
.height(100)
}
}
.backgroundColor(0xFFFFFF)
.width('100%')
.margin({
top: 10,
bottom: 10
})
}
}, (item: string) => item.toString())
}
.scrollBar(BarState.Off)
.height('100%')
.width('100%')
}
.backgroundColor(0xF1F3F5)
.padding(12)
}
}
在HarmonyOS Next中,手风琴动画可通过ArkUI的组件动画实现。使用Column或List容器,结合transition属性定义展开/收缩的动画效果。通过animateTo函数控制状态切换,设置height、opacity等属性的过渡。示例代码片段:
// 控制展开状态
@State isExpanded: boolean = false;
// 点击触发动画
animateTo({ duration: 300 }, () => {
this.isExpanded = !this.isExpanded;
})
在组件中绑定状态,动态调整高度即可实现手风琴效果。


