#鸿蒙通关秘籍#在鸿蒙系统中如何通过组件组合实现翻页动效?

HarmonyOS
2024-12-03 11:51:30
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
SMTP碧空如洗

在鸿蒙系统中,通过组件组合可以实现流畅的翻页动效,具体实现步骤如下:

  1. 定义一个包含旋转属性的BookPage组件并确定接收的参数。

    @Component
    struct BookPage {
      @Prop pageNum: number;
      @Prop rotateAngle: number;
      @Prop positionX: string;
      @Prop positionY: string;
    
      build() {
        Text(`${this.pageNum}`)
          .fontSize($r('app.integer.common_font_size'))
          .fontColor(Color.White)
          .fontWeight(FontWeight.Bold)
          .textAlign(TextAlign.Center)
          .backgroundColor($r('app.color.common_color_dark_blue'))
          .width($r('app.string.common_text_width'))
          .height($r('app.string.common_text_height'))
          .borderRadius($r('app.integer.common_border_radius'))
          .rotate({
            x: 0,
            y: 1,
            z: 0,
            angle: this.rotateAngle,
            centerX: this.positionX,
            centerY: this.positionY,
          })
      }
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
  2. 通过Stack组件将页面布局分为上下两层,其中包含两个Row,分别用于翻页动效。

    Stack() {
      Row() {
        BookPage({
          pageNum: this.pageNumTextC,
          rotateAngle: this.originalAngle,
          positionX: this.leftX,
          positionY: this.leftY
        })
        BookPage({
          pageNum: this.nextPageNumTextD,
          rotateAngle: this.originalAngle,
          positionX: this.leftX,
          positionY: this.leftY
        })
      }
      Row() {
        BookPage({
          pageNum: this.pageNumTextA,
          rotateAngle: this.rotateAngleTextA,
          positionX: this.centerX,
          positionY: this.centerY
        })
        BookPage({
          pageNum: this.animatePageNumTextB,
          rotateAngle: this.rotateAngleTextB,
          positionX: this.leftX,
          positionY: this.leftY
        })
      }
      Divider().strokeWidth(5).color(Color.White).height($r('app.string.divider_height')).vertical(true)
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
    • 28.
    • 29.
    • 30.
    • 31.
  3. 在页面出现时利用setInterval调用翻页动效函数pageTurningAnimate,通过animateTo实现旋转效果。

    aboutToAppear(): void {
      setInterval(() => {
        this.pageTurningAnimate();
      }, 1000)
    }
    
    private pageTurningAnimate() {
      animateTo(
        { duration: 700, onFinish: () => {
          this.pageNumTextA = this.animatePageNumTextB;
          this.rotateAngleTextA = 180;
          this.rotateAngleTextB = 0;
          this.animatePageNumTextB = (this.animatePageNumTextB + 1) % 8;
        }},
        () => {
          this.rotateAngleTextB = 180;
          this.nextPageNumTextD = this.animatePageNumTextB + 1;
        })
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
分享
微博
QQ
微信
回复
2024-12-03 14:11:46
相关问题