HarmonyOS/OpenHarmony应用开发-ArkTS触摸事件onTouch

鸿蒙时代
发布于 2023-3-13 09:31
浏览
0收藏

一、示例效果图片
HarmonyOS/OpenHarmony应用开发-ArkTS触摸事件onTouch-鸿蒙开发者社区HarmonyOS/OpenHarmony应用开发-ArkTS触摸事件onTouch-鸿蒙开发者社区
HarmonyOS/OpenHarmony应用开发-ArkTS触摸事件onTouch-鸿蒙开发者社区
五、示例代码
HarmonyOS/OpenHarmony应用开发-ArkTS触摸事件onTouch-鸿蒙开发者社区
Touch.ets

@Component
export default struct Touch {
  @State text: string = '';
  @State eventType: string = '';

  build() {
    Column() {
      Button('Touch').height(40).width(100)
        .onTouch((event: TouchEvent) => {
          if (event.type === TouchType.Down) {
            this.eventType = 'Down';
          }
          if (event.type === TouchType.Up) {
            this.eventType = 'no touch';
          }
          if (event.type === TouchType.Move) {
            this.eventType = 'Move';
          }
          this.text = 'TouchType:' + this.eventType + '\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;
        })
      Button('Touch').height(50).width(200).margin(20)
        .onTouch((event: TouchEvent) => {
          if (event.type === TouchType.Down) {
            this.eventType = 'Down';
          }
          if (event.type === TouchType.Up) {
            this.eventType = 'no touch';
          }
          if (event.type === TouchType.Move) {
            this.eventType = 'Move';
          }
          this.text = 'TouchType:' + this.eventType + '\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;
        })
      Text(this.text).fontSize(24)
    }.width('100%').height('100%').padding(30)
  }
}

index.ets

import touch from '../eventMuster/Touch'

@Entry
@Component
struct Index {

  build() {
    Column() {
      touch()
    }
    .width('100%')
    .height('100%')
  }
}

六、完整代码下载地址
https://gitee.com/jltfcloudcn/jump_to/tree/master/touch

分类
标签
HarmonyOSOpenHarmony应用开发-ArkTS触摸事.docx 71.42K 12次下载
收藏
回复
举报
回复
    相关推荐