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

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

HarmonyOS
2024-12-20 16:08:50
710浏览
收藏 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",
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 18:56:41


相关问题
键盘拉起时列表无法上下滑动
2909浏览 • 1回复 待解决
服务卡片可以响应上下滑动操作吗?
7733浏览 • 2回复 待解决
如何设置全屏返回
2585浏览 • 1回复 待解决
HarmonyOS SVGA图片加载
914浏览 • 1回复 待解决
HarmonyOS tabsgrid实现滑动
620浏览 • 1回复 待解决
HarmonyOS 属性如何打断
768浏览 • 1回复 待解决