HarmonyOS Scroll 横向滑动时根据滑动的距离触发视图不更新

HarmonyOS Scroll 横向滑动时根据滑动的距离触发视图不更新。

HarmonyOS
2024-11-27 08:41:54
1168浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考代码:

import display from '@ohos.display' 
import { BusinessError } from '@kit.BasicServicesKit' 
 
@Entry 
@Component 
struct Index { 
  private settings: RenderingContextSettings = new RenderingContextSettings(true) 
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 
  @State scrollValue: number = 0 
  size_11: number = vp2px(11) 
 
  @State screenWidth: number = 0 
 
  @State @Watch('draw')distance:number = 0 
 
  draw(){ 
    this.screenWidth = this.scrollValue 
    this.context.clearRect(0,0,this.screenWidth,200); 
    this.context.fillStyle = '#ff0000' 
    this.context.font = `normal normal ${this.size_11}px sans-serif` 
    this.context.textBaseline = 'middle' 
    let text = '哈' 
    this.context.fillText(text, this.scrollValue + 100, 100) 
 
 
    let text1 = '哈' 
    this.context.fillText(text1, 100, 200) 
  } 
  build() { 
    Column() { 
      Scroll() { 
        Column() { 
          Canvas(this.context) 
            .width(800)//.backgroundColor('#0000ff') 
            .onReady(() => { 
              this.draw() 
            }) 
        } 
      } 
      .height(400) 
      .scrollable(ScrollDirection.Horizontal) 
      .backgroundColor('#ff00fc37') 
      .scrollBar(BarState.Off) 
      .onScroll((xOffset: number, yOffset: number) => { 
        this.scrollValue = this.scrollValue + xOffset + 10 
        if (this.scrollValue < 0) { 
          this.scrollValue = 0 
        } 
        console.info('滚动距离:' + xOffset + '.. ' + this.scrollValue) 
        this.distance = this.distance+xOffset 
      }) 
    } 
 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-11-27 14:44:37
相关问题
HarmonyOS Tabs和横向Scroll滑动冲突
821浏览 • 1回复 待解决
HarmonyOS Grid横向滑动
1179浏览 • 1回复 待解决
HarmonyOS 视图更新
540浏览 • 1回复 待解决
HarmonyOS image图片纵向横向滑动
591浏览 • 1回复 待解决
HarmonyOS Scroll组件无法滑动
1191浏览 • 1回复 待解决
HarmonyOS scroll滑动问题
1125浏览 • 1回复 待解决
Tabs滑动距离问题有哪些?
1327浏览 • 1回复 待解决
scroll和list嵌套滑动
2555浏览 • 1回复 待解决
Scroll与WaterFlow滑动嵌套
2067浏览 • 1回复 待解决
如何监听List组件滑动距离
3477浏览 • 1回复 待解决
HarmonyOS Scroll嵌套List滑动问题
785浏览 • 1回复 待解决
HarmonyOS scroll嵌套List不能整体滑动
1343浏览 • 1回复 待解决