提交 6b7bfa02 编写于 作者: T tianyu

gesture cherryPick 3.2beta3

Signed-off-by: Ntianyu <tianyu55@h-partners.com>
上级 55be4fac
...@@ -36,24 +36,31 @@ LongPressGesture(value?: { fingers?: number, repeat?: boolean, duration?: number ...@@ -36,24 +36,31 @@ LongPressGesture(value?: { fingers?: number, repeat?: boolean, duration?: number
@Entry @Entry
@Component @Component
struct LongPressGestureExample { struct LongPressGestureExample {
@State count: number = 0 @State count: number = 0;
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Text('LongPress onAction:' + this.count) Text('LongPress onAction:' + this.count).fontSize(28)
// 单指长按文本触发该手势事件
.gesture(
LongPressGesture({ repeat: true })
// 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
.onAction((event: GestureEvent) => {
if (event.repeat) {
this.count++;
}
})
// 长按动作一结束触发
.onActionEnd(() => {
this.count = 0;
})
)
} }
.height(200).width(300).padding(60).border({ width:1 }).margin(30) .height(200)
.gesture( .width(300)
LongPressGesture({ repeat: true }) .padding(20)
// 长按动作存在会连续触发 .border({ width: 3 })
.onAction((event: GestureEvent) => { .margin(30)
if (event.repeat) { this.count++ }
})
// 长按动作一结束触发
.onActionEnd(() => {
this.count = 0
})
)
} }
} }
``` ```
......
...@@ -73,30 +73,57 @@ PanGestureOptions(value?: { fingers?: number; direction?: PanDirection; distance ...@@ -73,30 +73,57 @@ PanGestureOptions(value?: { fingers?: number; direction?: PanDirection; distance
@Entry @Entry
@Component @Component
struct PanGestureExample { struct PanGestureExample {
@State offsetX: number = 0 @State offsetX: number = 0;
@State offsetY: number = 0 @State offsetY: number = 0;
@State positionX: number = 0;
@State positionY: number = 0;
private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Left | PanDirection.Right });
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY) Column() {
} Text('PanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
.height(100).width(200).padding(20).border({ width: 1 }).margin(80) }
.translate({ x: this.offsetX, y: this.offsetY, z: 5 }) .height(200)
.gesture( .width(300)
PanGesture({}) .padding(20)
.border({ width: 3 })
.margin(50)
.translate({ x: this.offsetX, y: this.offsetY, z: 0 })
// 左右拖动触发该手势事件
.gesture(
PanGesture(this.panOption)
.onActionStart((event: GestureEvent) => { .onActionStart((event: GestureEvent) => {
console.info('Pan start') console.info('Pan start');
}) })
.onActionUpdate((event: GestureEvent) => { .onActionUpdate((event: GestureEvent) => {
this.offsetX = event.offsetX this.offsetX = this.positionX + event.offsetX;
this.offsetY = event.offsetY this.offsetY = this.positionY + event.offsetY;
}) })
.onActionEnd(() => { .onActionEnd(() => {
console.info('Pan end') this.positionX = this.offsetX;
this.positionY = this.offsetY;
console.info('Pan end');
})
)
Button('修改PanGesture触发条件')
.onClick(() => {
// 将PanGesture手势事件触发条件改为双指以任意方向拖动
this.panOption.setDirection(PanDirection.All);
this.panOption.setFingers(2);
}) })
) }
} }
} }
``` ```
![zh-cn_image_0000001174264374](figures/zh-cn_image_0000001174264374.gif) 示意图:
向左拖动:
![zh-cn_image_0000001174264374](figures/zh-cn_image_0000001174264374.png)
点击按钮修改PanGesture触发条件,双指向左下方拖动:
![zh-cn_image1_0000001174264374](figures/zh-cn_image1_0000001174264374.png)
\ No newline at end of file
...@@ -36,28 +36,42 @@ PinchGesture(value?: { fingers?: number, distance?: number }) ...@@ -36,28 +36,42 @@ PinchGesture(value?: { fingers?: number, distance?: number })
@Entry @Entry
@Component @Component
struct PinchGestureExample { struct PinchGestureExample {
@State scaleValue: number = 1 @State scaleValue: number = 1;
@State pinchValue: number = 1;
@State pinchX: number = 0;
@State pinchY: number = 0;
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Text('PinchGesture scale:' + this.scaleValue) Column() {
} Text('PinchGesture scale:\n' + this.scaleValue)
.height(100).width(200).padding(20).border({ width: 1 }).margin(80) Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')
.scale({ x: this.scaleValue, y: this.scaleValue, z: this.scaleValue }) }
.gesture( .height(200)
PinchGesture() .width(300)
.padding(20)
.border({ width: 3 })
.margin({ top: 100 })
.scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
// 三指捏合触发该手势事件
.gesture(
PinchGesture({ fingers: 3 })
.onActionStart((event: GestureEvent) => { .onActionStart((event: GestureEvent) => {
console.info('Pinch start') console.info('Pinch start');
}) })
.onActionUpdate((event: GestureEvent) => { .onActionUpdate((event: GestureEvent) => {
this.scaleValue = event.scale this.scaleValue = this.pinchValue * event.scale;
this.pinchX = event.pinchCenterX;
this.pinchY = event.pinchCenterY;
}) })
.onActionEnd(() => { .onActionEnd(() => {
console.info('Pinch end') this.pinchValue = this.scaleValue;
console.info('Pinch end');
}) })
) )
}.width('100%')
} }
} }
``` ```
![zh-cn_image_0000001174582848](figures/zh-cn_image_0000001174582848.gif) ![zh-cn_image_0000001174582848](figures/zh-cn_image_0000001174582848.png)
...@@ -36,28 +36,37 @@ RotationGesture(value?: { fingers?: number, angle?: number }) ...@@ -36,28 +36,37 @@ RotationGesture(value?: { fingers?: number, angle?: number })
@Entry @Entry
@Component @Component
struct RotationGestureExample { struct RotationGestureExample {
@State angle: number = 0 @State angle: number = 0;
@State rotateValue: number = 0;
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Text('RotationGesture angle:' + this.angle) Column() {
} Text('RotationGesture angle:' + this.angle)
.height(100).width(200).padding(20).border({ width:1 }) }
.margin(80).rotate({ x:1, y:2, z:3, angle: this.angle }) .height(200)
.gesture( .width(300)
.padding(20)
.border({ width: 3 })
.margin(80)
.rotate({ angle: this.angle })
// 双指旋转触发该手势事件
.gesture(
RotationGesture() RotationGesture()
.onActionStart((event: GestureEvent) => { .onActionStart((event: GestureEvent) => {
console.log('Rotation start') console.info('Rotation start');
}) })
.onActionUpdate((event: GestureEvent) => { .onActionUpdate((event: GestureEvent) => {
this.angle = event.angle this.angle = this.rotateValue + event.angle;
}) })
.onActionEnd(() => { .onActionEnd(() => {
console.log('Rotation end') this.rotateValue = this.angle;
console.info('Rotation end');
}) })
) )
}.width('100%')
} }
} }
``` ```
![zh-cn_image_0000001174264372](figures/zh-cn_image_0000001174264372.gif) ![zh-cn_image_0000001174264372](figures/zh-cn_image_0000001174264372.png)
...@@ -35,7 +35,6 @@ SwipeGesture(value?: { fingers?: number; direction?: SwipeDirection; speed?: num ...@@ -35,7 +35,6 @@ SwipeGesture(value?: { fingers?: number; direction?: SwipeDirection; speed?: num
| -------- | -------- | | -------- | -------- |
| onAction(event:(event?:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | 滑动手势识别成功回调。 | | onAction(event:(event?:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | 滑动手势识别成功回调。 |
![zh-cn_image_0000001231374559](figures/zh-cn_image_0000001231374661.png)
## 示例 ## 示例
```ts ```ts
...@@ -43,27 +42,31 @@ SwipeGesture(value?: { fingers?: number; direction?: SwipeDirection; speed?: num ...@@ -43,27 +42,31 @@ SwipeGesture(value?: { fingers?: number; direction?: SwipeDirection; speed?: num
@Entry @Entry
@Component @Component
struct SwipeGestureExample { struct SwipeGestureExample {
@State rotateAngle : number = 0 @State rotateAngle: number = 0;
@State speed : number = 1 @State speed: number = 1;
build() { build() {
Column() { Column() {
Text("SwipGesture speed : " + this.speed) Column() {
Text("SwipGesture angle : " + this.rotateAngle) Text("SwipeGesture speed\n" + this.speed)
} Text("SwipeGesture angle\n" + this.rotateAngle)
.position({x: 80, y: 200}) }
.border({width:2}) .border({ width: 3 })
.width(260).height(260) .width(300)
.rotate({x: 0, y: 0, z: 1, angle: this.rotateAngle}) .height(200)
.gesture( .margin(100)
SwipeGesture({fingers: 1, direction: SwipeDirection.Vertical}) .rotate({ angle: this.rotateAngle })
// 单指竖直方向滑动时触发该事件
.gesture(
SwipeGesture({ direction: SwipeDirection.Vertical })
.onAction((event: GestureEvent) => { .onAction((event: GestureEvent) => {
this.speed = event.speed this.speed = event.speed;
this.rotateAngle = event.angle this.rotateAngle = event.angle;
}) })
) )
}.width('100%')
} }
} }
``` ```
![zh-cn_image_0000001231374559](figures/zh-cn_image_0000001231374559.gif) ![zh-cn_image_0000001231374559](figures/zh-cn_image_0000001231374559.png)
...@@ -33,20 +33,25 @@ TapGesture(value?: { count?: number, fingers?: number }) ...@@ -33,20 +33,25 @@ TapGesture(value?: { count?: number, fingers?: number })
@Entry @Entry
@Component @Component
struct TapGestureExample { struct TapGestureExample {
@State value: string = '' @State value: string = '';
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Text('Click twice') // 单指双击文本触发手势事件
Text('Click twice').fontSize(28)
.gesture(
TapGesture({ count: 2 })
.onAction((event: GestureEvent) => {
this.value = JSON.stringify(event.fingerList[0]);
})
)
Text(this.value) Text(this.value)
} }
.height(200).width(300).padding(60).border({ width: 1 }).margin(30) .height(200)
.gesture( .width(300)
TapGesture({ count: 2 }) .padding(20)
.onAction(() => { .border({ width: 3 })
this.value = 'TapGesture onAction' .margin(30)
})
)
} }
} }
``` ```
......
...@@ -17,11 +17,11 @@ GestureGroup(mode: GestureMode, ...gesture: GestureType[]) ...@@ -17,11 +17,11 @@ GestureGroup(mode: GestureMode, ...gesture: GestureType[])
| gesture | [TapGesture](ts-basic-gestures-tapgesture.md)<br/>\|&nbsp;[LongPressGesture](ts-basic-gestures-longpressgesture.md)<br/>\|&nbsp;[PanGesture](ts-basic-gestures-pangesture.md)<br/>\|&nbsp;[PinchGesture](ts-basic-gestures-pinchgesture.md)<br/>\|&nbsp;[RotationGesture](ts-basic-gestures-rotationgesture.md) | 是 | - | 可变长参数,1个或者多个基础手势类型,这些手势会被组合识别。 | | gesture | [TapGesture](ts-basic-gestures-tapgesture.md)<br/>\|&nbsp;[LongPressGesture](ts-basic-gestures-longpressgesture.md)<br/>\|&nbsp;[PanGesture](ts-basic-gestures-pangesture.md)<br/>\|&nbsp;[PinchGesture](ts-basic-gestures-pinchgesture.md)<br/>\|&nbsp;[RotationGesture](ts-basic-gestures-rotationgesture.md) | 是 | - | 可变长参数,1个或者多个基础手势类型,这些手势会被组合识别。 |
## GestureMode枚举说明 ## GestureMode枚举说明
| 名称 | 描述 | | 名称 | 描述 |
| -------- | -------- | | -------- | -------- |
| Sequence | 顺序识别,按照手势的注册顺序识别手势,直到所有手势识别成功。当有一个手势识别失败时,所有手势识别失败。 | | Sequence | 顺序识别,按照手势的注册顺序识别手势,直到所有手势识别成功。当有一个手势识别失败时,所有手势识别失败。 |
| Parallel | 并发识别,注册的手势同时识别,直到所有手势识别结束,手势识别互相不影响。 | | Parallel | 并发识别,注册的手势同时识别,直到所有手势识别结束,手势识别互相不影响。 |
| Exclusive | 互斥识别,注册的手势同时识别,若有一个手势识别成功,则结束手势识别。 | | Exclusive | 互斥识别,注册的手势同时识别,若有一个手势识别成功,则结束手势识别。 |
## 事件 ## 事件
...@@ -38,43 +38,67 @@ GestureGroup(mode: GestureMode, ...gesture: GestureType[]) ...@@ -38,43 +38,67 @@ GestureGroup(mode: GestureMode, ...gesture: GestureType[])
@Entry @Entry
@Component @Component
struct GestureGroupExample { struct GestureGroupExample {
@State count: number = 0 @State count: number = 0;
@State offsetX: number = 0 @State offsetX: number = 0;
@State offsetY: number = 0 @State offsetY: number = 0;
@State borderStyles: BorderStyle = BorderStyle.Solid @State positionX: number = 0;
@State positionY: number = 0;
@State borderStyles: BorderStyle = BorderStyle.Solid;
build() { build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { Column() {
Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY) Text('sequence gesture\n' + 'LongPress onAction:' + this.count + '\nPanGesture offset:\nX: ' + this.offsetX + '\n' + 'Y: ' + this.offsetY)
}.translate({ x: this.offsetX, y: this.offsetY, z: 5 }) }
.height(100).width(200).padding(10).margin(80).border({ width: 1, style: this.borderStyles }) .translate({ x: this.offsetX, y: this.offsetY, z: 0 })
.height(150)
.width(200)
.padding(20)
.margin(20)
.border({ width: 3, style: this.borderStyles })
.gesture( .gesture(
GestureGroup(GestureMode.Sequence, //以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件
LongPressGesture({ repeat: true }) GestureGroup(GestureMode.Sequence,
.onAction((event: GestureEvent) => { LongPressGesture({ repeat: true })
if (event.repeat) {this.count++} .onAction((event: GestureEvent) => {
console.log('LongPress onAction') if (event.repeat) {
}) this.count++;
.onActionEnd(() => { }
console.log('LongPress end') console.info('LongPress onAction');
}), })
PanGesture({}) .onActionEnd(() => {
.onActionStart(() => { console.info('LongPress end');
this.borderStyles = BorderStyle.Dashed }),
console.log('pan start') PanGesture()
}) .onActionStart(() => {
.onActionUpdate((event: GestureEvent) => { this.borderStyles = BorderStyle.Dashed;
this.offsetX = event.offsetX console.info('pan start');
this.offsetY = event.offsetY })
console.log('pan update') .onActionUpdate((event: GestureEvent) => {
}) this.offsetX = this.positionX + event.offsetX;
) this.offsetY = this.positionY + event.offsetY;
console.info('pan update');
})
.onActionEnd(() => {
this.positionX = this.offsetX;
this.positionY = this.offsetY;
this.borderStyles = BorderStyle.Solid;
console.info('pan end');
})
)
.onCancel(() => { .onCancel(() => {
console.log('sequence gesture canceled') console.info('sequence gesture canceled');
}) })
) )
} }
} }
``` ```
![zh-cn_image_0000001174104384](figures/zh-cn_image_0000001174104384.gif) 示意图:
按顺序首先触发长按事件:
![zh-cn_image_0000001174104384](figures/zh-cn_image_0000001174104384.png)
按顺序首先触发长按事件,长按事件识别结束之后,其次触发拖动事件,向右下方拖动:
![zh-cn_image1_0000001174104384](figures/zh-cn_image1_0000001174104384.png)
\ No newline at end of file
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
## GestureType ## GestureType
| 名称 | 描述 | | 名称 | 描述 |
| -------- | -------- | | -------- | -------- |
| [TapGesture](ts-basic-gestures-tapgesture.md) | 点击手势,支持单次点击、多次点击识别。 | | [TapGesture](ts-basic-gestures-tapgesture.md) | 点击手势,支持单次点击、多次点击识别。 |
| [LongPressGesture](ts-basic-gestures-longpressgesture.md) | 长按手势。 | | [LongPressGesture](ts-basic-gestures-longpressgesture.md) | 长按手势。 |
| [PanGesture](ts-basic-gestures-pangesture.md) | 平移手势,滑动最小距离为5vp时识别成功。 | | [PanGesture](ts-basic-gestures-pangesture.md) | 平移手势,滑动最小距离为5vp时识别成功。 |
...@@ -32,10 +32,10 @@ ...@@ -32,10 +32,10 @@
## GestureMask枚举说明 ## GestureMask枚举说明
| 名称 | 描述 | | 名称 | 描述 |
| -------- | -------- | | -------- | -------- |
| Normal | 不屏蔽子组件的手势,按照默认手势识别顺序进行识别。 | | Normal | 不屏蔽子组件的手势,按照默认手势识别顺序进行识别。 |
| IgnoreInternal | 屏蔽子组件的手势,仅当前容器的手势进行识别。<br/>子组件上系统内置的手势不会被屏蔽,如子组件为List组件时,内置的滑动手势仍然会触发。 | | IgnoreInternal | 屏蔽子组件的手势,仅当前容器的手势进行识别。<br/>子组件上系统内置的手势不会被屏蔽,如子组件为List组件时,内置的滑动手势仍然会触发。 |
## 响应手势事件 ## 响应手势事件
...@@ -48,36 +48,36 @@ ...@@ -48,36 +48,36 @@
| onAction((event?:GestureEvent)&nbsp;=&gt;&nbsp;void) | Tap手势识别成功回调。 | | onAction((event?:GestureEvent)&nbsp;=&gt;&nbsp;void) | Tap手势识别成功回调。 |
## GestureEvent对象说明 ## GestureEvent对象说明
| 名称 | 类型 | 描述 | | 名称 | 类型 | 描述 |
| -------- | -------- | -------- | | -------- | -------- | -------- |
| timestamp<sup>8+</sup> | number | 事件时间戳。 | | timestamp<sup>8+</sup> | number | 事件时间戳。 |
| target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md#eventtarget8对象说明) | 触发手势事件的元素对象显示区域。 | | target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md#eventtarget8对象说明) | 触发手势事件的元素对象显示区域。 |
| source<sup>8+</sup> | [SourceType](#sourcetype枚举说明) | 事件输入设备。 | | source<sup>8+</sup> | [SourceType](#sourcetype枚举说明) | 事件输入设备。 |
| repeat | boolean | 是否为重复触发事件,用于LongPressGesture手势触发场景。 | | repeat | boolean | 是否为重复触发事件,用于LongPressGesture手势触发场景。 |
| fingerList<sup>8+</sup> | [FingerInfo](#fingerinfo对象说明)[] | 触发事件的所有手指信息,用于LongPressGesture与TapGesture手势触发场景。 | | fingerList<sup>8+</sup> | [FingerInfo](#fingerinfo对象说明)[] | 触发事件的所有手指信息,用于LongPressGesture与TapGesture手势触发场景。 |
| offsetX | number | 手势事件x轴相对偏移量,单位为vp,用于PanGesture手势触发场景,从左向右滑动offsetX为正,反之为负。 | | offsetX | number | 手势事件x轴相对偏移量,单位为vp,用于PanGesture手势触发场景,从左向右滑动offsetX为正,反之为负。 |
| offsetY | number | 手势事件y轴相对偏移量,单位为vp,用于PanGesture手势触发场景,从上向下滑动offsetY为正,反之为负。 | | offsetY | number | 手势事件y轴相对偏移量,单位为vp,用于PanGesture手势触发场景,从上向下滑动offsetY为正,反之为负。 |
| angle | number | 用于RotationGesture手势触发场景时,表示旋转角度;用于SwipeGesture手势触发场景时,表示滑动手势的角度,即两根手指间的线段与水平方向的夹角变化的度数。<br/>>&nbsp;&nbsp;**说明:**<br/>>&nbsp;角度计算方式:滑动手势被识别到后,连接两根手指之间的线被识别为起始线条,随着手指的滑动,手指之间的线条会发生旋转,根据起始线条两端点和当前线条两端点的坐标,使用反正切函数分别计算其相对于水平方向的夹角,最后arctan2(cy2-cy1,cx2-cx1)-arctan2(y2-y1,x2-x1)为旋转的角度。以起始线条为坐标系,顺时针旋转为0到180度,逆时针旋转为-180到0度。 | | angle | number | 用于RotationGesture手势触发场景时,表示旋转角度;用于SwipeGesture手势触发场景时,表示滑动手势的角度,即两根手指间的线段与水平方向的夹角变化的度数。<br/>>&nbsp;&nbsp;**说明:**<br/>>&nbsp;角度计算方式:滑动手势被识别到后,连接两根手指之间的线被识别为起始线条,随着手指的滑动,手指之间的线条会发生旋转,根据起始线条两端点和当前线条两端点的坐标,使用反正切函数分别计算其相对于水平方向的夹角,最后arctan2(cy2-cy1,cx2-cx1)-arctan2(y2-y1,x2-x1)为旋转的角度。以起始线条为坐标系,顺时针旋转为0到180度,逆时针旋转为-180到0度。 |
| speed<sup>8+</sup> | number | 滑动手势速度,即所有手指滑动的平均速度,单位为vp/秒,用于SwipeGesture手势触发场景。 | | speed<sup>8+</sup> | number | 滑动手势速度,即所有手指滑动的平均速度,单位为vp/秒,用于SwipeGesture手势触发场景。 |
| scale | number | 缩放比例,用于PinchGesture手势触发场景。 | | scale | number | 缩放比例,用于PinchGesture手势触发场景。 |
| pinchCenterX | number | 捏合手势中心点相对于当前组件元素左上角x轴坐标,单位为vp,用于PinchGesture手势触发场景。 | | pinchCenterX | number | 捏合手势中心点相对于当前组件元素左上角x轴坐标,单位为vp,用于PinchGesture手势触发场景。 |
| pinchCenterY | number | 捏合手势中心点相对于当前组件元素左上角y轴坐标,单位为vp,用于PinchGesture手势触发场景。 | | pinchCenterY | number | 捏合手势中心点相对于当前组件元素左上角y轴坐标,单位为vp,用于PinchGesture手势触发场景。 |
## SourceType枚举说明 ## SourceType枚举说明
| 名称 | 描述 | | 名称 | 描述 |
| -------- | -------- | | -------- | -------- |
| Unknown | 未知设备。 | | Unknown | 未知设备。 |
| Mouse | 鼠标。 | | Mouse | 鼠标。 |
| TouchScreen | 触摸屏。 | | TouchScreen | 触摸屏。 |
## FingerInfo对象说明 ## FingerInfo对象说明
| 名称 | 类型 | 描述 | | 名称 | 类型 | 描述 |
| -------- | -------- | -------- | | -------- | -------- | -------- |
| id | number | 手指的索引编号。 | | id | number | 手指的索引编号。 |
| globalX | number | 相对于应用窗口左上角的x轴坐标。 | | globalX | number | 相对于应用窗口左上角的x轴坐标。 |
| globalY | number | 相对于应用窗口左上角的y轴坐标。 | | globalY | number | 相对于应用窗口左上角的y轴坐标。 |
| localX | number | 相对于当前组件元素左上角的x轴坐标。 | | localX | number | 相对于当前组件元素左上角的x轴坐标。 |
| localY | number | 相对于当前组件元素左上角的y轴坐标。 | | localY | number | 相对于当前组件元素左上角的y轴坐标。 |
## 示例 ## 示例
...@@ -87,28 +87,51 @@ ...@@ -87,28 +87,51 @@
@Entry @Entry
@Component @Component
struct GestureSettingsExample { struct GestureSettingsExample {
@State value: string = '' @State priorityTestValue: string = '';
@State parallelTestValue: string = '';
build() { build() {
Column(){ Column() {
Column() { Column() {
Text('Click\n' + this.value) Text('TapGesture:' + this.priorityTestValue).fontSize(28)
.gesture( .gesture(
TapGesture() TapGesture()
.onAction(() => { .onAction(() => {
this.value = 'gesture onAction' this.priorityTestValue += '\nText';
})) }))
}.height(200).width(300).padding(60).border({ width: 1 }) }
//设置为priorityGesture时,会优先识别该绑定手势忽略内部gesture手势 .height(200)
.width(250)
.padding(20)
.margin(20)
.border({ width: 3 })
// 设置为priorityGesture时,点击文本会忽略Text组件的TapGesture手势事件,优先识别父组件Column的TapGesture手势事件
.priorityGesture( .priorityGesture(
TapGesture() TapGesture()
.onAction((event: GestureEvent) => { .onAction((event: GestureEvent) => {
this.value = 'priorityGesture onAction' + '\ncomponent globalPos:(' this.priorityTestValue += '\nColumn';
+ event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:' }), GestureMask.IgnoreInternal)
+ event.target.area.width + '\nheight:' + event.target.area.height
}), GestureMask.IgnoreInternal Column() {
) Text('TapGesture:' + this.parallelTestValue).fontSize(28)
}.padding(60) .gesture(
TapGesture()
.onAction(() => {
this.parallelTestValue += '\nText';
}))
}
.height(200)
.width(250)
.padding(20)
.margin(20)
.border({ width: 3 })
// 设置为parallelGesture时,点击文本会同时触发子组件Text与父组件Column的TapGesture手势事件
.parallelGesture(
TapGesture()
.onAction((event: GestureEvent) => {
this.parallelTestValue += '\nColumn';
}), GestureMask.Normal)
}
} }
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册