实现思路如下:
完整示例参考如下:
@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, (_: string, index: number) => { 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); } }); }, (_: 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%' ); }); } }