实现思路如下:
防止出现闪动,animateTo中参数duration数值需要保持和Swiper组件属性duration数值保持一致。
@Entry @Component struct SwiperDemo { private swiperController: SwiperController = new SwiperController(); private data: string[] = ['0', '1', '2', '3', '4', '5', '6']; @State currentIndex: number = 0; // 当前页面 build() { Column({ space: 25 }) { Swiper(this.swiperController) { ForEach(this.data, (item: string) => { Column() { Text(item).width(40).height(40).textAlign(TextAlign.Center).fontSize(30); } .width('100%') .height('100%') .border({ width: 3, color: '#ff24d8e5' }); }); } .displayMode(SwiperDisplayMode.STRETCH) .displayCount(1) // 设置Swiper视窗内元素显示个数 .loop(false) .index(this.currentIndex) .cachedCount(2) .indicator(true) .duration(500) // 设置子组件切换的动画时长 .nextMargin(this.currentIndex <= 2 ? 50 : 0) // nextMargin属性来控制后边距 .curve(Curve.Linear) .backgroundColor('#ffbbfce2') .onAnimationStart((targetIndex: number) => { // 在onAnimationStart回调中结合animateTo方法来更新当前轮播页的索引 this.getUIContext()?.animateTo ({ duration: 500, curve: Curve.Linear, playMode: PlayMode.Normal, }, () => { this.currentIndex = targetIndex; }); }); }.width('100%').height('20%').margin({ top: 5 }); } }