示例代码参考如下:
@Entry
@Component
struct SpringAnimation {
data: Array<number> = [0, 1, 2, 3, 4, 5];
@State scaleNumber: number = 1;
build() {
Column({ space: 10 }) {
// 构建内容组件
ForEach(this.data, (index: number) => {
Column() {
Text('欢迎使用HarmonyOS-' + index);
}
.height('10%')
.width('90%')
.borderRadius(12)
.backgroundColor('#f1f3f5')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.scale({ y: 1 / this.scaleNumber, centerY: 0 });
});
}
.height('100%')
.width('100%')
.scale({ y: this.scaleNumber, centerY: 0 }) // 设置纵向拉伸以及拉伸中心点
.gesture(
PanGesture()
.onActionUpdate((event) => {
// 通过滑动距离设置拉伸倍率
if (event.offsetX > 0 && event.offsetX < 100) {
// 向下滑动且控制最大响应滑动距离100和最大伸缩倍率1.5
this.scaleNumber = event.offsetX * 0.005 + 1;
}
})
.onActionEnd(() => {
// 拉伸结束后返回原大小
this.getUIContext()?.animateTo({
duration: 200,
playMode: PlayMode.Normal,
}, () => {
this.scaleNumber = 1;
});
})
);
}
}