1# 自定义事件分发 2 3ArkUI在处理触屏事件时,会在触屏事件触发前进行按压点和组件区域的触摸测试,来收集需要响应触屏事件的组件,再基于触摸测试结果分发相应的触屏事件。在父节点,开发者可以通过onChildTouchTest决定如何让子节点去做触摸测试,影响子组件的触摸测试,最终影响后续的触屏事件分发,具体影响参考[TouchTestStrategy](#touchteststrategy枚举说明)枚举说明。 4 5> **说明:** 6> 7> - 从API Version 11开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8> 9> - onClick以及旋转、捏合手势经过自定义事件分发之后可能会因为触摸热区没有命中导致事件不响应。 10 11## onChildTouchTest 12 13onChildTouchTest(event: (value: Array<TouchTestInfo>) => TouchResult): T 14 15当前组件可通过设置回调来自定义触摸测试,可以控制触摸测试中的子节点行为。 16 17**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 18 19**系统能力:** SystemCapability.ArkUI.ArkUI.Full 20 21**参数:** 22 23| 参数名 | 类型 | 必填 | 说明 | 24| ------ | ------------------------------------------ | ---- | ---------------------- | 25| value | Array<[TouchTestInfo>](#touchtestinfo) | 是 | 包含子节点信息的数组。 | 26 27**返回值:** 28 29| 类型 | 说明 | 30| -------- | -------- | 31| T | 返回当前组件。 | 32 33>**说明:** 34> 35>子节点信息数组中只包含命名节点的信息,即开发者通过id属性设置了id的节点。 36 37## TouchTestInfo 38 39当前屏幕触点所在组件的坐标系、id和尺寸相关信息。 40 41**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 42 43**系统能力:** SystemCapability.ArkUI.ArkUI.Full 44 45| 名称 | 类型 | 描述 | 46| ------------- | ------ | ---------------------------------------- | 47| windowX | number | 按压点相对于窗口左上角的x轴坐标。<br />单位:vp | 48| windowY | number |按压点相对于窗口左上角的y轴坐标。<br />单位:vp| 49| parentX | number |按压点相对于父组件左上角的x轴坐标。<br />单位:vp | 50| parentY | number |按压点相对于父组件左上角的y轴坐标。<br />单位:vp | 51| x | number | 按压点相对于子组件左上角的x轴坐标。<br />单位:vp | 52| y | number | 按压点相对于子组件左上角的y轴坐标。<br />单位:vp | 53| rect | [RectResult](ts-types.md#rectresult10) |子组件的大小。 | 54| [id](ts-universal-attributes-component-id.md) | string | 通过id属性设置的组件id。 | 55 56## TouchResult 57 58自定义事件分发结果,开发者通过返回结果来影响事件分发。 59 60**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 61 62**系统能力:** SystemCapability.ArkUI.ArkUI.Full 63 64| 名称 | 类型 | 必填 | 描述 | 65| --------- | --------- | ---- |--------------------------------------- | 66| strategy | [TouchTestStrategy](#touchteststrategy枚举说明) | 是 | 事件派发策略。 | 67| id | string | 否 | 通过id属性设置的组件id。<br>当strategy为TouchTestStrategy.DEFAULT时,id是可选的;当strategy是TouchTestStrategy.FORWARD_COMPETITION或TouchTestStrategy.FORWARD时,id是必需的(如果没有返回id,则当成TouchTestStrategy.DEFAULT处理)。 | 68 69## TouchTestStrategy枚举说明 70 71事件派发策略。 72 73**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 74 75**系统能力:** SystemCapability.ArkUI.ArkUI.Full 76 77| 名称 | 描述 | 78| ------------| ----------------------------------------- | 79| DEFAULT | 自定义分发不产生影响,系统按当前节点命中状态分发事件。 | 80| FORWARD_COMPETITION | 应用指定分发事件到某个子节点,其他兄弟节点是否分发事件交由系统决定。 | 81| FORWARD | 应用指定分发事件到某个子节点,系统不再处理分发事件到其他兄弟节点。 | 82 83## 示例 84 85### 示例1(设置事件派发策略为FORWARD_COMPETITION) 86 87该示例点击List下方空白区域后拖动,能够拖动List滑动。点击Button按钮,Button会响应onClick事件。 88 89```ts 90// xxx.ets 91import { PromptAction } from '@kit.ArkUI'; 92 93@Entry 94@Component 95struct ListExample { 96 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 97 promptAction: PromptAction = this.getUIContext().getPromptAction(); 98 @State text: string = 'Button' 99 100 build() { 101 Column() { 102 List({ space: 12, initialIndex: 0 }) { 103 ForEach(this.arr, (item: number) => { 104 ListItem() { 105 Text('Item ' + item) 106 .width('100%') 107 .height(56) 108 .fontSize(16) 109 .textAlign(TextAlign.Start) 110 }.borderRadius(24) 111 .backgroundColor(Color.White) 112 .padding({ left: 12, right: 12 }) 113 }, (item: string) => item) 114 } 115 .listDirection(Axis.Vertical) 116 .scrollBar(BarState.Off) 117 .edgeEffect(EdgeEffect.Spring) 118 .onScrollIndex((start: number, end: number) => { 119 console.info('first' + start) 120 console.info('last' + end) 121 }) 122 .onDidScroll((scrollOffset: number, scrollState: ScrollState) => { 123 console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset) 124 }) 125 .width('100%') 126 .height('65%') 127 .id('MyList') 128 129 Button(this.text) 130 .width(312) 131 .height(40) 132 .id('Mybutton') 133 .fontSize(16) 134 .fontWeight(FontWeight.Medium) 135 .margin({ top: 80 }) 136 .onClick(() => { 137 this.text = 'click the button' 138 this.promptAction.showToast({ message: 'you click the button.', duration: 3000 }) 139 }) 140 } 141 .width('100%') 142 .height('100%') 143 .backgroundColor(0xF1F3F5) 144 .justifyContent(FlexAlign.End) 145 .padding({ left: 12, right: 12, bottom: 24 }) 146 .onChildTouchTest((touchinfo) => { 147 for (let info of touchinfo) { 148 if (info.id == 'MyList') { 149 return { id: info.id, strategy: TouchTestStrategy.FORWARD_COMPETITION } 150 } 151 } 152 return { strategy: TouchTestStrategy.DEFAULT } 153 }) 154 } 155} 156``` 157 158 159 160### 示例2(设置事件派发策略为FORWARD) 161 162点击List下方空白区域后拖动,能够拖动List滑动。点击Button按钮,Button不会响应onClick事件。 163 164```ts 165// xxx.ets 166import { PromptAction } from '@kit.ArkUI'; 167 168@Entry 169@Component 170struct ListExample { 171 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 172 promptAction: PromptAction = this.getUIContext().getPromptAction(); 173 @State text: string = 'Button' 174 175 build() { 176 Column() { 177 List({ space: 12, initialIndex: 0 }) { 178 ForEach(this.arr, (item: number) => { 179 ListItem() { 180 Text('Item ' + item) 181 .width('100%') 182 .height(56) 183 .fontSize(16) 184 .textAlign(TextAlign.Start) 185 }.borderRadius(24) 186 .backgroundColor(Color.White) 187 .padding({ left: 12, right: 12 }) 188 }, (item: string) => item) 189 } 190 .listDirection(Axis.Vertical) 191 .scrollBar(BarState.Off) 192 .edgeEffect(EdgeEffect.Spring) 193 .onScrollIndex((start: number, end: number) => { 194 console.info('first' + start) 195 console.info('last' + end) 196 }) 197 .onDidScroll((scrollOffset: number, scrollState: ScrollState) => { 198 console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset) 199 }) 200 .width('100%') 201 .height('65%') 202 .id('MyList') 203 204 Button(this.text) 205 .width(312) 206 .height(40) 207 .id('Mybutton') 208 .fontSize(16) 209 .fontWeight(FontWeight.Medium) 210 .margin({ top: 80 }) 211 .onClick(() => { 212 this.text = 'click the button' 213 this.promptAction.showToast({ message: 'you click the button.', duration: 3000 }) 214 }) 215 } 216 .width('100%') 217 .height('100%') 218 .backgroundColor(0xF1F3F5) 219 .justifyContent(FlexAlign.End) 220 .padding({ left: 12, right: 12, bottom: 24 }) 221 .onChildTouchTest((touchinfo) => { 222 for (let info of touchinfo) { 223 if (info.id == 'MyList') { 224 return { id: info.id, strategy: TouchTestStrategy.FORWARD } 225 } 226 } 227 return { strategy: TouchTestStrategy.DEFAULT } 228 }) 229 } 230} 231``` 232 233 234 235### 示例3(设置事件派发策略为DEFAULT) 236 237点击List下方空白区域后拖动,List不会滑动。点击Button按钮,Button会响应onClick事件。 238 239```ts 240// xxx.ets 241import { PromptAction } from '@kit.ArkUI'; 242 243@Entry 244@Component 245struct ListExample { 246 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 247 promptAction: PromptAction = this.getUIContext().getPromptAction(); 248 @State text: string = 'Button' 249 250 build() { 251 Column() { 252 List({ space: 12, initialIndex: 0 }) { 253 ForEach(this.arr, (item: number) => { 254 ListItem() { 255 Text('Item ' + item) 256 .width('100%') 257 .height(56) 258 .fontSize(16) 259 .textAlign(TextAlign.Start) 260 }.borderRadius(24) 261 .backgroundColor(Color.White) 262 .padding({ left: 12, right: 12 }) 263 }, (item: string) => item) 264 } 265 .listDirection(Axis.Vertical) 266 .scrollBar(BarState.Off) 267 .edgeEffect(EdgeEffect.Spring) 268 .onScrollIndex((start: number, end: number) => { 269 console.info('first' + start) 270 console.info('last' + end) 271 }) 272 .onDidScroll((scrollOffset: number, scrollState: ScrollState) => { 273 console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset) 274 }) 275 .width('100%') 276 .height('65%') 277 .id('MyList') 278 279 Button(this.text) 280 .width(312) 281 .height(40) 282 .id('Mybutton') 283 .fontSize(16) 284 .fontWeight(FontWeight.Medium) 285 .margin({ top: 80 }) 286 .onClick(() => { 287 this.text = 'click the button' 288 this.promptAction.showToast({ message: 'you click the button.', duration: 3000 }) 289 }) 290 } 291 .width('100%') 292 .height('100%') 293 .backgroundColor(0xF1F3F5) 294 .justifyContent(FlexAlign.End) 295 .padding({ left: 12, right: 12, bottom: 24 }) 296 .onChildTouchTest((touchinfo) => { 297 return { strategy: TouchTestStrategy.DEFAULT } 298 }) 299 } 300} 301``` 302 303