HarmonyOS swiper使用customContentTransition后显示异常(卡片盖在了前一个卡片上,而不是切换)?

@Entry
@Component
struct Index {
  @State cardChangeAnimationDuration: number = 0
  swiperController: SwiperController = new SwiperController()
  @State autoPlay: boolean = true
  userScrolled: boolean = false
  dataList: Color[] = [Color.Red, Color.Yellow, Color.Blue]

  aboutToAppear(): void {
    setInterval(() => {
      if (this.autoPlay) {
        this.swiperController.showNext()
        promptAction.showToast({ message: '切换下一个卡片' })
      }
    }, 2000);
  }

  build() {
    Stack() {
      Swiper(this.swiperController) {
        ForEach(this.dataList, (item: Color, index) => {
          Text(`卡片${index}`)
            .textAlign(TextAlign.Center)
            .font({ size: 17, weight: 500 })
            .backgroundColor(item)
            .opacity(0.5)
        })
      }
      .autoPlay(false) // 组件不自动轮播,自动轮播逻辑交由VM调用scrollToIndex/showNext实现
      .loop(this.autoPlay)
      .indicator(false)
      .duration(this.cardChangeAnimationDuration) // 切换时长可配置
      .curve(Curve.Linear) // 注:<=API12, duration生效需要配置.curve(Curve.Linear)
      .cachedCount(3)
      .width('100%')
      .height('100%')
      .nestedScroll(SwiperNestedScrollMode.SELF_FIRST)
      .onClick(() => {
        this.cardChangeAnimationDuration = !this.autoPlay ? 700 : 400
        this.autoPlay = !this.autoPlay
        if (this.autoPlay) {
          this.userScrolled = false
        }
      })
      .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
        this.userScrolled = true
      })
      .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
        if (extraInfo.currentOffset == 0 && this.userScrolled) {
          this.autoPlay = false
        }
      })
      .customContentTransition({
        timeout: 1000,
        transition: (proxy: SwiperContentTransitionProxy) => {

        }
      })
    }
  }
}
阅读 574
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进