Marquee组件在文本末尾滚动到控件末尾时,能停止滚动并触发回调事件(目前是滚动到控件的开头)

文字轮播卡片,需要在文字滚动到控件末尾的时候,停止滚动,并等待其他文字轮播的动画停止,当所有的轮播动画都到达控件的末尾时,同时切换文字内容,进行下一轮的轮播。

HarmonyOS
2024-10-11 10:28:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

可以使用scroll自行封装的替代方案,参考下面demo:

@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World';  
  @State textList: string[] = [  
    'this is a test string1 this is a test string1 this is a test string1',  
    'this is a test string2 this is a test string2',  
    'this is a test string3 this is a test string3 this is a test string3 this is a test string3',  
  ]  
  
  build() {  
    Row() {  
      Column() {  
        myMarqueeCard({  
          textList: this.textList,  
        })  
      }  
      .width('100%')  
      .margin(20)  
    }  
    .height('100%')  
  }  
}  
  
@Component  
struct myMarqueeCard {  
  @Prop textList: string[]  
  scroller1: Scroller = new Scroller()  
  scroller2: Scroller = new Scroller()  
  scroller3: Scroller = new Scroller()  
  
  build() {  
    Column() {  
      this.SingleText(this.textList[0], this.scroller1)  
      this.SingleText(this.textList[1], this.scroller2)  
      this.SingleText(this.textList[2], this.scroller3)  
    }  
  }  
  
  @Builder  
  SingleText(text: string, scroller: Scroller) {  
    Scroll(scroller) {  
      Row() {  
        Text(text).fontSize(30)  
      }  
    }  
    .width(300)  
    .scrollable(ScrollDirection.Horizontal)  
    .enableScrollInteraction(false)  
    .scrollBar(BarState.Off)  
    .onAppear(() => {  
      this.handleScroll(scroller)  
    })  
  }  
  
  handleScroll(scroller: Scroller) {  
    let timer: number = setInterval(() => {  
      const curOffset: OffsetResult = scroller.currentOffset()  
      scroller.scrollTo({  
        xOffset: curOffset.xOffset + 50, yOffset: curOffset.yOffset, animation: {  
          duration: 1000,  
          curve: Curve.Linear  
        }  
      })  
      if (scroller.isAtEnd()) {  
        clearInterval(timer);  
        if (this.scroller1.isAtEnd() && this.scroller2.isAtEnd() && this.scroller3.isAtEnd()) {  
          // 其他操作  
        }  
      }  
    }, 500)  
  }  
}

Scroll参考: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5

分享
微博
QQ
微信
回复
2024-10-11 17:29:09
相关问题
list组件无法滚动到底部
1285浏览 • 1回复 待解决
怎么判断webview滚动到最下方?
438浏览 • 2回复 待解决
Web组件怎么知道滚动到顶部了
870浏览 • 1回复 待解决
HarmonyOS swiper如何滚动到任意页面?
484浏览 • 1回复 待解决
Text文本过长如何实现上下滚动
656浏览 • 1回复 待解决
如何获取List组件滚动滚动距离
2581浏览 • 1回复 待解决