鸿蒙NEXT版实战开发:Marquee组件的文字滚动,第一次滚动出现大量空白,如何避免空白出现?

往期鸿蒙全套实战文章必看:(附带鸿蒙全栈学习资料)


Marquee组件的文字滚动,第一次滚动出现大量空白,如何避免空白出现

问题现象

Marquee组件在文本滚动时,文本滚动到控件的开头,会造成大量空白,如何实现让文本末尾滚动到控件末尾时停止,避免空白出现。

解决措施

Marquee暂不支持在文本末尾停止,推荐使用Scroll代替跑马灯组件实现文字滚动。

示例代码如下:

@Entry
@Component
struct MarqueeScroll {
  @State textList: string[] = [
    'this is a test string1 this is a test string1',
    'this is a test string2 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'
  ];
  @State count: number = 1;

  build() {
    Row() {
      Column() {
        myMarqueeCard({
          textList: $textList,
          updateList: () => {
            this.textList = [
              `这是测试数据${this.count++}这是测试数据${this.count++}`,
              `这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}`,
              `这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}这是测试数据${this.count++}`
            ];
          }
        })
      }
      .width('100%')
      .margin(20)
    }
    .height('100%')
  }
}

@Component
struct myMarqueeCard {
  @Link @Watch('handleNewList') textList: string[];
  @State list: string[] = [];
  scroller1: Scroller = new Scroller();
  scroller2: Scroller = new Scroller();
  scroller3: Scroller = new Scroller();
  updateList?: () => void;

  handleNewList() {
    console.info(JSON.stringify(this.textList));
  }

  build() {
    Column() {
      this.SingleText({ text: this.textList[0], scroller: this.scroller1 })
      this.SingleText({ text: this.textList[1], scroller: this.scroller2 })
      this.SingleText({ text: this.textList[2], scroller: this.scroller3 })
    }
  }

  @Builder
  SingleText($$: Tmp) {
    Scroll($$.scroller) {
      Row() {
        Text($$.text)
          .fontSize(30)
          .onAppear(() => {
            this.handleScroll($$.scroller);
          })
      }
    }
    .width(300)
    .scrollable(ScrollDirection.Horizontal)
    .enableScrollInteraction(false)
    .scrollBar(BarState.Off)

  }

  handleScroll(scroller: Scroller) {
    setInterval(() => {
      const curOffset: OffsetResult = scroller.currentOffset();
      scroller.scrollTo({
        xOffset: curOffset.xOffset + 50, yOffset: curOffset.yOffset, animation: {
          duration: 1000,
          curve: Curve.Linear
        }
      });
      if (scroller.isAtEnd()) {
        if (this.scroller1.isAtEnd() && this.scroller2.isAtEnd() && this.scroller3.isAtEnd()) {
          if (this.updateList) {
            this.scroller1.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 0 } });
            this.scroller2.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 0 } });
            this.scroller3.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 0 } });
            this.updateList();
          }
        }
      }
    }, 500);
  }
}

class Tmp {
  text: string = '';
  scroller: Scroller = new Scroller();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值