1# 自定义事件拦截
2
3为组件提供自定义的事件拦截能力,开发者可根据事件在控件上按下时发生的位置,输入源等事件信息决定控件上的HitTestMode属性。
4
5>  **说明:**
6>
7>  从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9
10## onTouchIntercept
11
12onTouchIntercept(callback: Callback<TouchEvent, HitTestMode>)
13
14**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
15
16**系统能力:** SystemCapability.ArkUI.ArkUI.Full
17
18**参数:**
19
20| 参数名        | 参数类型                    | 必填  | 参数描述                          |
21| ---------- | -------------------------- | ------- | ----------------------------- |
22| callback      | Callback<[TouchEvent](ts-universal-events-touch.md#touchevent对象说明), [HitTestMode](ts-universal-attributes-hit-test-behavior.md#HitTestMode枚举说明)> | 是     |  给组件绑定自定义事件拦截回调,并使能在做触摸测试时回调此函数。 |
23
24
25## 示例
26
27该示例通过onTouchIntercept修改组件的HitTestMode属性。
28
29```ts
30// xxx.ets
31@Entry
32@Component
33struct Index {
34  isPolygon(event: TouchEvent) {
35    return true;
36  }
37
38  build(){
39    Row(){
40      Column(){
41        Text("hello world")
42          .backgroundColor(Color.Blue)
43          .fontSize(50)
44          .fontWeight(FontWeight.Bold)
45          .onClick(()=>{
46            console.log("Text click");
47          })
48      }
49      .width(400)
50      .height(300)
51      .backgroundColor(Color.Pink)
52      .onClick(()=>{
53        console.log("Column click");
54      })
55      // 调用onTouchIntercept修改该组件的HitTestMode属性
56      .onTouchIntercept((event : TouchEvent) => {
57        console.log("OnTouchIntercept + " + JSON.stringify(event));
58        if (this.isPolygon(event)) {
59          return HitTestMode.None
60        }
61        return HitTestMode.Default
62      })
63    }
64    .width('100%')
65  }
66}
67```
68