HarmonyOS 关于ontouch如何获取到触摸与抬起对应的item?

HarmonyOS 关于ontouch如何获取到触摸与抬起对应的item?

HarmonyOS
2024-10-18 09:43:47
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

onTouch需要绑定在grid上,通过event.touches[0].x、event.touches[0].y和item大小之间的关系计算得到touchUp所在的位置,也可以通过touchMove实时更改滑过组件的状态,touchMove更改子组件状态参考如下:

const childWidth: number = 60  
const childHeight: number = 60  
@Observed  
export class girdData {  
  message: string;  
  background: Color;  
  index: number;  
  selected: boolean;  
  constructor(message: string, index: number) {  
    this.message = message  
    this.background = Color.Gray;  
    this.index = index;  
    this.selected = false;  
  }  
}  
@Observed  
export class InfoList extends Array<girdData> {  
}  
;  
@Component  
struct viewA {  
  @ObjectLink info: girdData;  
  build() {  
    GridItem() {  
      Text(this.info.message)  
        .fontSize(16)  
        .backgroundColor(this.info.background)  
        .width('100%')  
        .height('100%')  
        .textAlign(TextAlign.Center)  
    }  
    .width(childWidth)  
    .height(childHeight)  
  }  
}  
@Component  
struct viewB {  
  @ObjectLink infoList: InfoList;  
  private tmpIndex: number = -1;  
  changeChildColor(event: TouchEvent, index: number) {  
    if (index !== this.infoList[Math.floor(event.touches[0].x / childWidth)].index) {  
      this.infoList[Math.floor(event.touches[0].x / childWidth)].selected =  
        !this.infoList[Math.floor(event.touches[0].x / childWidth)].selected  
      if (this.infoList[Math.floor(event.touches[0].x / childWidth)].selected) {  
        this.infoList[Math.floor(event.touches[0].x / childWidth)].background = Color.Orange  
      } else {  
        this.infoList[Math.floor(event.touches[0].x / childWidth)].background = Color.Gray  
      }  
      this.tmpIndex = this.infoList[Math.floor(event.touches[0].x / childWidth)].index  
    }  
  }  
  isOnTouchInParent(event: TouchEvent): boolean {  
    if (event.touches[0].x > 0 && event.touches[0].x <= childWidth * this.infoList.length &&  
      event.touches[0].y > 0 && event.touches[0].y < childWidth) {  
      return true;  
    }  
    return false  
  }  
  build() {  
    Grid() {  
      ForEach(this.infoList, (item: girdData) => {  
        viewA({ info: item })  
      }, (item: girdData) => JSON.stringify(item))  
    }  
    .columnsTemplate('1fr 1fr 1fr 1fr 1fr')  
    .rowsTemplate('1fr')  
    .backgroundColor(Color.Gray)  
    .height(childHeight)  
.width(childWidth * this.infoList.length)  
.onTouch((event: TouchEvent) => {  
  if (event.type === TouchType.Down) {  
  }  
  if (event.type === TouchType.Down) {  
    this.tmpIndex = -1;  
  }  
  if (event.type === TouchType.Move) {  
    if (this.isOnTouchInParent(event)) {  
      this.changeChildColor(event, this.tmpIndex)  
    }  
  }  
  console.log('TouchType:' + event.type + '\nDistance between touch point and touch element:\nx: '  
    + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('  
    + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'  
    + event.target.area.width + '\nheight:' + event.target.area.height)  
})  
  }  
}  
@Entry  
@Component  
struct Second {  
  @State infoList: InfoList = new InfoList();  
  aboutToAppear() {  
    for (let i = 0; i < 5; i++) {  
      this.infoList.push(new girdData(i.toString(), i));  
    }  
  }  
  build() {  
    Column() {  
      Row() {  
      }.height('30%').backgroundColor(Color.Gray)  
      viewB({ infoList: this.infoList })  
      Row() {  
        Text('滑动更改颜色').fontColor(Color.White).fontSize(14)  
      }.height('8%')  
      .margin(10)  
      .backgroundColor(Color.Brown)  
    }.align(Alignment.Center)  
    .width('100%')  
    .alignItems(HorizontalAlign.Center)  
  }  
}

onTouch事件参考链接: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-events-touch-V5#ontouch

分享
微博
QQ
微信
回复
2024-10-18 17:47:57
相关问题
关于 onArrange中获取到参数问题
4995浏览 • 1回复 待解决
HyperlinkonTouch预览报错
1988浏览 • 1回复 待解决
鸿蒙响应屏幕触摸事件如何获取
7494浏览 • 1回复 已解决
HarmonyOS 如何监听键盘抬起落下
22浏览 • 1回复 待解决
HarmonyOS 如何获取到页面堆栈对象
31浏览 • 1回复 待解决
HarmonyOS 关于AAIDdeviceid使用
252浏览 • 1回复 待解决
请问如何获取到鸿蒙根布局
6760浏览 • 1回复 待解决
hvigro中如何获取到buildMode
1967浏览 • 1回复 待解决
怎么获取List里面每个itemposition?
271浏览 • 1回复 待解决