HarmonyOS 如何实现滑动监听?

想监听手在屏幕上滑动的距离(水平距离和垂直的距离),怎么实现?

HarmonyOS
2024-10-17 11:39:43
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可通过scroller.currentOffset()获取当前List滚动的总距离。xOffset:水平方向偏移量,yOffset:垂直方向偏移量。

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-scroll-V5#currentoffset

参考代码:

@Entry  
@Component  
struct ScrollFontSizePage {  
  @State fontSize: number = 10  
  private msg: string = 'Scroll to Resize'  
  scroller: Scroller = new Scroller;  
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]  
  build() {  
    Column() {  
      Text(this.msg)  
        .fontSize(this.fontSize)  
        .fontWeight(FontWeight.Bold)  
      Scroll(this.scroller) {  
        Column() {  
          ForEach(this.arr, (item: number) => {  
            Text(item.toString())  
              .width('90%')  
              .height(200)  
              .backgroundColor(0xFFFFFF)  
              .borderWidth(1)  
              .borderColor(Color.Black)  
              .borderRadius(15)  
              .fontSize(16)  
              .textAlign(TextAlign.Center)  
          }, (item: string) => item)  
        }.width('100%').backgroundColor(0xDCDCDC)  
      }  
      .backgroundColor(Color.Yellow)  
      .height(600)  
      .edgeEffect(EdgeEffect.Spring)  
      .onScrollStop(() => {  
        let offset = this.scroller.currentOffset(); //返回当前的滚动偏移量  
        //xOffset:水平方向偏移量,yOffset:垂直方向偏移量  
        console.log(`scroll cur ${offset.xOffset}, ${offset.yOffset}`)  
        this.fontSize = 26 - (2600 - offset.yOffset) / 100  
      })  
  
      Row() {  
        Button('Top').fontSize(20).fontWeight(FontWeight.Bold).onClick(() => {  
          this.scroller.scrollTo({ xOffset: 0, yOffset: 0, animation: { duration: 1000, curve: Curve.Ease } })  
        }  
        )  
        Button('Mid').fontSize(20).fontWeight(FontWeight.Bold).onClick(() => {  
          this.scroller.scrollTo({ xOffset: 0, yOffset: 400, animation: { duration: 1000, curve: Curve.Ease } })  
        }  
        )  
        Button('Bottom').fontSize(20).fontWeight(FontWeight.Bold).onClick(() => {  
          this.scroller.scrollTo({ xOffset: 0, yOffset: 1800, animation: { duration: 1000, curve: Curve.Ease } })  
        }  
        )  
      }  
    }  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-17 18:02:05


相关问题
如何监听Slider滑动结束
1210浏览 • 1回复 待解决
HarmonyOS 如何监听屏幕滑动pop功能
616浏览 • 1回复 待解决
HarmonyOS 监听List组件滑动
926浏览 • 1回复 待解决
如何监听List组件的总滑动距离
3423浏览 • 1回复 待解决
HarmonyOS 如何实现滑动游标尺
447浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
1245浏览 • 1回复 待解决
监听pdf是否已经滑动到最底部失败
2203浏览 • 1回复 待解决
如何实现对LocalStorage的实时监听
1106浏览 • 1回复 待解决
HarmonyOS 手势滑动登录UI实现
753浏览 • 1回复 待解决
HarmonyOS tabs和grid实现滑动
591浏览 • 1回复 待解决
如何实现纵向且逆向滑动的Slider?
1008浏览 • 1回复 待解决
Canvas制作图表如何实现滑动惯性?
1265浏览 • 1回复 待解决
求大佬告知如何实现录音监听
2829浏览 • 1回复 待解决