HarmonyOS 类似翻页效果实现

HarmonyOS
2024-12-20 17:59:40
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit
@Entry
@Component
struct NovelPage {
  @Provide('fontSize') @Watch('onFontSizeChange') fontSize: number = Constants.INIT_FONT_SIZE;
  @Provide('bgColorIndex') @Watch('onBgColorChanged') bgColorIndex: BGColorType = BGColorType.WHITE;
  @Provide('bgColor') bgColor: string = BG_COLOR_ARRAY[BGColorType.WHITE];
  @Provide('offsetX') offsetX: number = 0
  @Provide('offsetY') offsetY: number = 0;
  @Provide('screenH') screenH: number = 0;
  @Provide('screenW') screenW: number = 0;
  @Provide('sumRow') sumRow: number = 0;
  @Provide('rowWord') rowWord: number = 0;
  @Provide('rotateAngleOne') rotateAngleOne: number = Constants.INIT_ROTATE_ANGLE_ONE;
  @Provide('rotateAngleTwo') rotateAngleTwo: number = 0.0;
  @Provide('curPosition') curPosition: number = 0;
  @Provide('turnStyle') turnStyle: FlipPageType = FlipPageType.SLIDE_FLIP_PAGE;
  @Provide('currentPageNum') @Watch('onFlush') currentPageNum: number = 1;
  @Provide('pageWordSum') pageWordSum: number = 0;
  @Provide('light') light: number = Constants.INIT_SCREEN_LIGHT;
  @Provide('isSystemLight') isSystemLight: boolean = false;
  @Provide('rowGap') rowGap: number = Constants.INIT_ROW_GAP;
  @State currentStartIndex: number = 0;
  @State isShow: boolean = false;
  @State isFontChanged: boolean = false;

  aboutToAppear(): void {
    this.screenW = px2vp(display.getDefaultDisplaySync().width);
    this.screenH = px2vp(display.getDefaultDisplaySync().height - (AppStorage.get('avoidHeight') as number));
    this.sumRow = Math.floor((this.screenH) / (this.fontSize + this.rowGap));
    this.rowWord = Math.floor((this.screenW - Constants.SCREEN_MARGIN_LEFT * 2) / this.fontSize);
    this.simulatePageContent();
    this.changeSystemBarStatue();
  }

  onFontSizeChange() {
    this.sumRow = Math.floor((this.screenH) / (this.fontSize + this.rowGap));
    this.rowWord = Math.floor((this.screenW - Constants.SCREEN_MARGIN_LEFT * 2) / this.fontSize);
    let pageWordSum = this.sumRow * this.rowWord;

    if (this.currentStartIndex > pageWordSum) {
      this.currentPageNum = Math.floor(this.currentStartIndex / (pageWordSum)) +
        (this.currentStartIndex > 1 && this.currentStartIndex % pageWordSum > 0 ? 2 : 1);
    } else if (this.currentStartIndex > 0) {
      this.currentPageNum = 2;
    } else {
      Logger.info('currentStartIndex = ' + this.currentStartIndex);
    }
    this.isFontChanged = true;
    this.simulatePageContent();
  }

  changeSystemBarStatue(): void {
    window.getLastWindow(getContext(this), (err, data) => {
      const errCode = err.code;
      if (errCode) {
        return;
      }
      let SystemBarProperties: window.SystemBarProperties = {
        statusBarColor: BG_COLOR_ARRAY[this.bgColorIndex],
        navigationBarColor: BG_COLOR_ARRAY[this.bgColorIndex],
        navigationBarContentColor: Constants.TRANSPARENT
      };
      try {
        data.setWindowSystemBarProperties(SystemBarProperties, (err: BusinessError) => {
          const errCode: number = err.code;
          if (errCode) {
            Logger.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
            return;
          }
          data.setWindowBackgroundColor(BG_COLOR_ARRAY[this.bgColorIndex]);
        });
      } catch (exception) {
        Logger.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(exception));
      }
    });
  }

  onBgColorChanged() {
    this.changeSystemBarStatue();
  }

  onFlush() {
    Logger.info('currentPageNum=' + this.currentPageNum + ', isFontChanged=' + this.isFontChanged);
    if (this.isFontChanged && this.currentPageNum === 1) {
      this.isFontChanged = false;
      this.currentStartIndex = 0;
    }
  }

  simulatePageContent() {
    this.offsetY = 0;
    this.rotateAngleTwo = 0.0;
    this.rotateAngleOne = Constants.INIT_ROTATE_ANGLE_ONE;
  }

  private clickAnimateTo(isLeft: Boolean) {
    if (this.turnStyle === FlipPageType.SLIDE_FLIP_PAGE) {
      animateTo({
        duration: Constants.SLIDE_DURATION,
        curve: Curve.EaseOut,
        onFinish: () => {
          if (this.offsetX > 0) {
            this.currentPageNum > 0 ? this.currentPageNum - 1 : this.currentPageNum;
            this.currentStartIndex -= this.sumRow * this.rowWord;
          }
          if (this.offsetX < 0) {
            this.currentPageNum += 1;
            this.currentStartIndex += this.sumRow * this.rowWord;
          }
          Logger.info(this.currentStartIndex.toString());
          this.offsetX = 0;
          this.simulatePageContent();
        }
      }, () => {
        if (isLeft) {
          this.offsetX = this.screenW;
        } else {
          this.offsetX = -this.screenW;
        }
      })
    }
  }

  build() {
    Row() {

      if (this.turnStyle === FlipPageType.SLIDE_FLIP_PAGE) {
        SlideFlipView({
          currentStartIndex: this.currentStartIndex
        })
      }

    }

    .width(Constants.FULL_PERCENT)
    .height(Constants.FULL_PERCENT)
    .bindSheet(
      $$this.isShow,
      this.myBuilder(),
      {
        height: SheetSize.FIT_CONTENT,
        detents: [Constants.SHEET_HEIGHT, Constants.SHEET_HEIGHT + 1],
        showClose: true,
        dragBar: true,
        title: { title: Constants.SHEET_TITLE },
        backgroundColor: Constants.SHEET_BACKGROUND_COLOR
      }
    )
    .backgroundColor(this.bgColor)
  }

  @Builder
  myBuilder() {


  }
}
 
接上一条:Reade.ets

@Component
export default struct Reader {
  @Consume('bgColor') @Watch('onPageChange') bgColor: string;
  @Consume('fontSize') @Watch('onPageChange') fontSize: number;
  @Consume('turnStyle') turnStyle: FlipPageType;
  @Consume('screenW') screenW: number;
  @Consume('screenH') screenH: number;
  @Consume('rowGap') rowGap: number;
  @Consume('sumRow') sumRow: number
  @Consume('rowWord') rowWord: number;
  @Prop @Watch('onPageChange') startIndex: number = 0;
  private settings: RenderingContextSettings = new RenderingContextSettings(true);
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
  private wordWidth: number = 0;
  private wordHeight: number = 0;

  aboutToAppear(): void {
    this.drawText(this.startIndex);
  }

  onPageChange() {
    this.drawText(this.startIndex);
  }

  drawText(startIndex: number) {
    this.wordWidth = this.fontSize;
    this.wordHeight = this.fontSize;
    this.context.fillStyle = this.bgColor;
    this.context.fillRect(0, 0, this.screenW, this.screenH);
    this.context.fillStyle = Color.Black;
    this.context.font = vp2px(this.fontSize) + Constants.CANVAS_FONT_SET;
    if (startIndex < 0) {
      startIndex = 0;
    }

    let gap = ((this.screenW - Constants.SCREEN_MARGIN_LEFT * 2) - this.wordWidth * this.rowWord) / (this.rowWord - 1);
    let realRowGap = (this.screenH - this.sumRow * (this.wordHeight + this.rowGap)) / (this.sumRow - 1);
    let currentX = Constants.SCREEN_MARGIN_LEFT;
    let currentY = this.wordHeight;
    for (let i = startIndex;; i++) {
      if (currentX + this.wordWidth > this.screenW - (Constants.SCREEN_MARGIN_LEFT - 1)) {
        currentX = Constants.SCREEN_MARGIN_LEFT;
        currentY = currentY + this.rowGap + this.wordHeight + realRowGap;
        if (currentY > this.screenH) {
          break;
        }
      }
      this.context.fillText(Constants.TEXT.charAt(i % Constants.TEXT.length), currentX, currentY);
      currentX += this.wordWidth + gap;
    }
  }

  build() {
    Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Start, justifyContent: FlexAlign.Start }) {
      Column() {
        Canvas(this.context)
          .width(Constants.FULL_PERCENT)
          .height(Constants.FULL_PERCENT)
          .onReady(() => {
            this.drawText(this.startIndex);
          })
      }
      .width(Constants.FULL_PERCENT)
    }
    .height(Constants.FULL_PERCENT)
  }
}
 
第三条:SlideFlipView.ets

@Component
export struct SlideFlipView {
  @Consume('offsetX') offsetX: number;
  @Consume('sumRow') sumRow: number;
  @Consume('rowWord') rowWord: number;
  @Consume('screenW') screenW: number;
  @Consume('currentPageNum') currentPageNum: number;
  @Link currentStartIndex: number;
  private isFirst: boolean = false;

  build() {
    Stack() {
      Reader({ startIndex: this.currentStartIndex + this.sumRow * this.rowWord })
        .translate({ x: this.offsetX >= 0 ? this.screenW : this.screenW + this.offsetX, y: 0, z: 0 })

      Reader({ startIndex: this.currentStartIndex })
        .translate({ x: this.offsetX, y: 0, z: 0 })
        .width(this.screenW)

      Reader({ startIndex: this.currentStartIndex - this.sumRow * this.rowWord })
        .translate({ x: this.offsetX >= 0 ? -this.screenW + this.offsetX : -this.screenW, y: 0, z: 0 })
    }
    .gesture(
      PanGesture()
        .onActionUpdate((event?: GestureEvent) => {
          if (!event) {
            return;
          }
          if (this.currentPageNum <= 1 && event.offsetX > 0) {
            this.isFirst = true;
            return;
          }

          this.offsetX = event.offsetX;
        })
        .onActionEnd(() => {
          animateTo({
            duration: Constants.FLIP_DURATION,
            curve: Curve.EaseOut,
            onFinish: () => {
              if (this.offsetX > 0) {
                this.currentPageNum -= 1;
                if (this.currentStartIndex !== 0) {
                  this.currentStartIndex -= this.sumRow * this.rowWord;
                }
              }
              if (this.offset
  • 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.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
分享
微博
QQ
微信
回复
2024-12-20 19:13:48


相关问题
HarmonyOS 效果实现方案
1082浏览 • 1回复 待解决
HarmonyOS 动画效果实现
620浏览 • 1回复 待解决
HarmonyOS list选中效果实现
278浏览 • 1回复 待解决
HarmonyOS 首页轮播效果实现
303浏览 • 1回复 待解决
HarmonyOS clipShape 动画效果实现
165浏览 • 0回复 待解决
PopWindow的效果实现有哪些?
965浏览 • 1回复 待解决
HarmonyOS 如何实现数据翻页效果
498浏览 • 1回复 待解决
引导遮罩效果实现的最佳方案
1780浏览 • 1回复 待解决
动态布局下加载loading效果实现
1268浏览 • 1回复 待解决
HarmonyOS如何实现阅读器翻页效果
1093浏览 • 1回复 待解决
HarmonyOS 怎么实现类似SnackBar的效果
422浏览 • 1回复 待解决
如何实现类似keyframes的效果
2148浏览 • 1回复 待解决