未验证 提交 53829f71 编写于 作者: L luoying_ace 提交者: Gitee

update zh-cn/application-dev/reference/arkui-ts/ts-universal-events-touch.md.

Signed-off-by: Nluoying_ace <luoying19@huawei.com>
上级 2106fec1
# 触摸事件 # 触摸事件
当手指放在组件上、滑动或从组件上移开时触发。 当手指在组件上按下、滑动、抬起时触发。
> **说明:** > **说明:**
> >
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
| 名称 | 是否冒泡 | 功能描述 | | 名称 | 是否冒泡 | 功能描述 |
| ------------------------------------------------------------ | -------- | ------------------------------------------------------------ | | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
| onTouch(event:&nbsp;(event?:&nbsp;TouchEvent)&nbsp;=&gt;&nbsp;void) | 是 | 触摸动作触发该方法调用,event参数[TouchEvent](#touchevent对象说明)介绍。 | | onTouch(event:&nbsp;(event?:&nbsp;TouchEvent)&nbsp;=&gt;&nbsp;void) | 是 | 手指触摸动作触发该回调,event返回值[TouchEvent](#touchevent对象说明)介绍。 |
## TouchEvent对象说明 ## TouchEvent对象说明
...@@ -45,29 +45,45 @@ ...@@ -45,29 +45,45 @@
@Entry @Entry
@Component @Component
struct TouchExample { struct TouchExample {
@State text: string = '' @State text: string = '';
@State eventType: string = '' @State eventType: string = '';
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Button('Touch').backgroundColor(0x2788D9).height(40).width(80) Button('Touch').height(40).width(100)
.onTouch((event: TouchEvent) => { .onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) { if (event.type === TouchType.Down) {
this.eventType = 'Down' this.eventType = 'Down';
} }
if (event.type === TouchType.Up) { if (event.type === TouchType.Up) {
this.eventType = 'Up' this.eventType = 'Up';
} }
if (event.type === TouchType.Move) { if (event.type === TouchType.Move) {
this.eventType = 'Move' this.eventType = 'Move';
} }
console.info(this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: ' this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
+ event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\ncomponent globalPos:(' + 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.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
+ event.target.area.width + '\nheight:' + event.target.area.height) + 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 = 'Up';
}
if (event.type === TouchType.Move) {
this.eventType = 'Move';
}
this.text = 'TouchType:' + this.eventType + '\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;
}) })
Text(this.text) Text(this.text)
}.height(200).width(350).padding({ left: 35, right: 35, top: 35 }) }.width('100%').padding(30)
} }
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册