滑动选择组件,有谁知道怎么解决吗?


HarmonyOS
2024-05-26 15:24:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

本文主要将一组组件排列,通过在上面滑动,将滑动的组件选中。

使用的核心API

触摸事件

核心代码解释

1.排列子组件

 Row() { 
  
          ForEach(this.text_list1, (value: string,idx:number) => { 
            Text(value) 
              .width(this.childWidth) 
              .height(this.childHeight) 
              .fontSize(20) 
              .backgroundColor(this.color_list1[Math.floor(8*Math.random())]) 
              .textAlign(TextAlign.Center) 
          }) 
        } 
        .height(this.childHeight) 
        .width(this.childWidth*this.text_list1.length)

2.给父组件添加onTouch事件,根据滑动的坐标记录子组件

 .onTouch((event: TouchEvent) => { 
          if (event.type === TouchType.Down) { 
            if (this.isOnTouchInParent(event)) { 
              this.displayMsg = '#<' 
              this.textTemp = this.getChildText(event) 
              this.displayMsg = this.displayMsg + this.textTemp 
            } 
          } 
          if (event.type === TouchType.Up) { 
            if (this.isOnTouchInParent(event)) { 
              this.displayMsg = this.displayMsg + '>' 
              this.textTemp = '' 
            } 
  
          } 
          if (event.type === TouchType.Move) { 
            if (this.isOnTouchInParent(event)) { 
              if (this.textTemp === this.getChildText(event)) { 
  
              }else { 
                this.textTemp = this.getChildText(event) 
                this.displayMsg = this.displayMsg + this.textTemp 
              } 
  
            } 
          } 
        })

3.判断子组件是否合规与重复

isOnTouchInParent(event: TouchEvent) :boolean{ 
    if (event.touches[0].x>0 &&event.touches[0].x<=this.childWidth*this.text_list1.length && event.touches[0].y>0&&event.touches[0].y<this.childWidth) { 
      return true; 
    } 
    return false 
  } 
  getChildText(event: TouchEvent) :string{ 
    return this.text_list1[Math.floor(event.touches[0].x/this.childWidth)] 
  }
分享
微博
QQ
微信
回复
2024-05-27 20:25:57
相关问题
有谁知道沙箱目录怎么获取
801浏览 • 1回复 待解决
有谁知道web组件如何下拉刷新
520浏览 • 1回复 待解决
有谁知道ArkTS支持定时器
883浏览 • 1回复 待解决
动态页面跳转方案,有谁知道
571浏览 • 1回复 待解决
有谁知道ArkTS支持隐式跳转
518浏览 • 1回复 待解决
设备唯一ID,有谁知道怎么处理?
507浏览 • 1回复 待解决
有谁知道flexBasis使用问题
415浏览 • 1回复 待解决
有谁知道如何生成UUID
459浏览 • 1回复 待解决
JSVM使用示例,有谁知道
398浏览 • 1回复 待解决
同进程多Hap问题 ,有谁知道
405浏览 • 1回复 待解决
有谁知道如何主动关闭应用
595浏览 • 1回复 待解决
有谁知道应用升级的方式
646浏览 • 1回复 待解决
有谁知道Image图片取反色
698浏览 • 1回复 待解决
有谁知道常用AppFreeze使用指导
196浏览 • 1回复 待解决
有谁知道可以直接使用so库
453浏览 • 1回复 待解决
有谁知道怎么解决啊?
442浏览 • 1回复 待解决
vp、fp、px的区别,有谁知道
701浏览 • 1回复 待解决