Scroll滚动到顶/底部后,下/上拖执行自定义方法

问题现象

Scroll滚动到顶部时继续下拖和滚动到底部时继续上拖,如何执行自定义方法?

背景知识

解决方案

使用PanGesture为Scroll绑定平移事件,在滑动结束即onActionEnd中判断Scroll位置以进行相应处理。

@Entry
@Component
struct ScrollDemo {
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Vertical });
  private scrollerForScroll: Scroller = new Scroller();

  build() {
    Column() {
      Scroll(this.scrollerForScroll) {
        // 此处'app.media.startIcon'仅作示例
        Image($r('app.media.startIcon'))
          .size({ width: 400, height: 1050 });
      }
      .width('100%')
      .height('100%')
      .edgeEffect(EdgeEffect.Spring)
      .parallelGesture(
        PanGesture(this.panOption)
          .onActionEnd((event: GestureEvent) => {
            console.info('PanGesture end');
            // 滑动到底部并且继续滑动
            if (this.scrollerForScroll.isAtEnd() && event.offsetY < 0) {
              console.info('执行自定义操作1');
            }
            // 滑动到顶部并且继续滑动
            const offsetRes = this.scrollerForScroll.currentOffset(); // 获取Scroll现在的位置
            if (offsetRes.yOffset <= 0 && event.offsetY > 0) {
              console.info('执行自定义操作2');
            }
          }), GestureMask.Normal);
    };
  }
}