HarmonyOS Grid上下滑动的动效和需求的差距很大

Grid上下滑动的动效和需求的差距很大

HarmonyOS
2024-12-20 16:08:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

参考DEMO:

@Entry
@Component
export struct Page240605101412064_temp {
  weekDatas: string[] = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  // private startDay: Date = new Date(2024, 4, 1) // 当前显示的月份的第一天
  data: Array<CalendaEntity> = [];
  @State gridTop: number = 0
  @State blankTop: number = 0
  private endBlankY: number = 0
  private endGridY: number = 0
  private isStart: boolean = true
  private scroller: Scroller = new Scroller()
  private today: Date = new Date()
  private startDay: Date = new Date(this.today.getFullYear(), this.today.getMonth(), 1)

  aboutToAppear(): void {
    const monthDataWithWeekday = this.calcDatas(this.startDay)
    monthDataWithWeekday.forEach(item => {
      console.log("日期是》》》》》》》" + item.year, ' ', item.month, ' ', item.day, ' ', item.weekday);
    });

  }

  calcDatas(startDate: Date): Array<CalendaEntity> {
    const endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0);
    let currentDate = new Date(startDate);
    while (currentDate <= endDate) {
      const weekday = currentDate.getDay();
      const weekdayNames = [0, 1, 2, 3, 4, 5, 6];
      const weekdayStr = weekdayNames[weekday];
      let year = currentDate.getFullYear()
      let month = currentDate.getMonth() + 1
      let day = currentDate.getDate().toString()
      if (day === this.today.getDate().toString()) {
        day = '今';
      }
      this.data.push({
        year,
        month,
        day,
        weekday: weekdayStr
      });
      currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + 1)
    }
    if (this.data.length !== 0) {
      let firstWeek = this.data[0].weekday;
      let tempData: Array<CalendaEntity> = [];
      while (firstWeek > 0) {
        tempData.push(new CalendaEntity());
        firstWeek--;
      }
      this.data = tempData.concat(this.data);
      let lastWeek = this.data[this.data.length - 1].weekday;
      while (lastWeek < 6) {
        this.data.push(new CalendaEntity())
        lastWeek++;
      }
    }
    return this.data;
  }

  build() {
    Column() {
      Row() {
        ForEach(this.weekDatas, (item: string) => {
          Text(item)
            .textAlign(TextAlign.Center)
            .fontColor('#9E9E9E')
            .fontSize(14)
            .layoutWeight(1)
        })
      }
      // .backgroundColor()
      .alignItems(VerticalAlign.Center)
      .height(60)

      Scroll(this.scroller) {
        Column() {
          Grid() {
            ForEach(this.data, (item: CalendaEntity) => {
              GridItem() {
                Stack() {
                  Image("")
                    .backgroundColor('#35cfba')
                    .borderRadius(100)
                    .width(34)
                    .height(34)
                    .visibility((item.day === "0" || item.day !== '今') ? Visibility.Hidden : Visibility.Visible)
                  Column() {
                    Text(item.day === "0" ? " " : item.day)
                      .fontSize(14)
                      .fontColor(Color.Gray)
                      .width('14%')
                      .textAlign(TextAlign.Center)
                    Text()
                      .backgroundColor(Color.Red)
                      .width(4)
                      .height(4)
                      .borderRadius(50)
                      .visibility(item.day === "0" ? Visibility.Hidden : Visibility.Visible)
                  }
                }
              }.onClick(() => {
                console.log("dddddddddd");
              })
            })
          }.rowsGap(20)
          // .columnsGap(10)
          .backgroundColor('#fff')
          .margin({ top: this.gridTop })
          .onAreaChange((oldValue: Area, newValue: Area) => {
            console.log('h', newValue.height)
            // h 304
          })


          Blank().layoutWeight(1).backgroundColor(Color.Gray)
            .margin({ top: this.blankTop })
        }.height('calc(100% + 560vp)')
      }
      .height('100%')
      .scrollBar(BarState.Off)
      .onReachStart(() => {
        this.isStart = true
      })
      .onScrollFrameBegin((offset: number, state: ScrollState) => {
        console.log('offset', offset)
        if (this.isStart) {
          return { offsetRemain: 0 }
        }
        if (!this.scroller.currentOffset().yOffset) {
          this.isStart = false
        }
        return { offsetRemain: offset }
      })
      .parallelGesture(
        PanGesture({ direction: PanDirection.Vertical, distance: 1 })
          .onActionStart((event: GestureEvent) => {
            console.info('Pan start')
          })
          .onActionUpdate((event: GestureEvent) => {
            console.log('event.offsetY', event.offsetY)
            console.log('this.isStart', this.isStart)
            if (event && this.isStart) {
              if (this.blankTop == 0) {
                if (this.endGridY == 0) {
                  this.gridTop = this.endGridY + event.offsetY < -(304 / 6 * 2) ? -(304 / 6 * 2) :
                    this.endGridY + event.offsetY > 0 ? 0 : this.endGridY + event.offsetY
                  console.log('this.gridTop', this.gridTop)
                } else {
                  this.gridTop = this.endBlankY + this.endGridY + event.offsetY < -(304 / 6 * 2) ? -(304 / 6 * 2) :
                    this.endBlankY + this.endGridY + event.offsetY > 0 ? 0 :
                      this.endBlankY + this.endGridY + event.offsetY
                  console.log('this.gridTop', this.gridTop)
                }

              }
              if (this.gridTop == -(304 / 6 * 2)) {
                if (this.endBlankY == 0) {
                  this.blankTop =
                    this.endBlankY + event.offsetY + (304 / 6 * 2) < -(304 / 6 * 3) ? -(304 / 6 * 3) :
                      this.endBlankY + event.offsetY + (304 / 6 * 2) > 0 ? 0 :
                        this.endBlankY + event.offsetY + (304 / 6 * 2)
                  console.log('this.blankTop', this.blankTop)
                } else {
                  this.blankTop =
                    this.endBlankY + event.offsetY < -(304 / 6 * 3) ? -(304 / 6 * 3) :
                      this.endBlankY + event.offsetY > 0 ? 0 :
                        this.endBlankY + event.offsetY
                  console.log('this.blankTop', this.blankTop)
                }

              }
            }
          })
          .onActionEnd((event: GestureEvent) => {
            this.endBlankY = this.blankTop
            this.endGridY = this.gridTop
            console.log('this.endBlankY', this.endBlankY)
            console.log('this.endGridY', this.endGridY)
          })
      )

    }.width('100%')

    // .height(330)

  }
}

export class CalendaEntity {
  year: number = 0;
  month: number = 0;
  day: string = "0";
  weekday: number = -1;
}

export enum OptMode {
  WEEK = "week",
  MONTH = "month",
}
分享
微博
QQ
微信
回复
2024-12-20 18:56:41
相关问题
键盘拉起时列表无法上下滑动
2446浏览 • 1回复 待解决
服务卡片可以响应上下滑动操作吗?
7307浏览 • 2回复 待解决
如何设置全屏返回
2181浏览 • 1回复 待解决
HarmonyOS SVGA图片加载
468浏览 • 1回复 待解决
HarmonyOS tabsgrid实现滑动
315浏览 • 1回复 待解决
HarmonyOS 属性如何打断
289浏览 • 1回复 待解决
HarmonyOS 是否有办法实现SVGA播放
362浏览 • 1回复 待解决