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)  
  }  
}
  • 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.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.

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中获取到参数问题
5632浏览 • 1回复 待解决
HarmonyOS 关于获取缓存清除缓存
557浏览 • 1回复 待解决
HarmonyOS 如何获取对应设备信息
523浏览 • 1回复 待解决
鸿蒙响应屏幕触摸事件如何获取
8310浏览 • 1回复 已解决
HyperlinkonTouch预览报错
2894浏览 • 1回复 待解决
HarmonyOS 如何获取到APP本身应用名称
1353浏览 • 1回复 待解决