harmony 鸿蒙Hit Test Control

  • 2025-06-12
  • 浏览 (1)

Hit Test Control

Hit test control allows you to configure hit testing for components. When processing a touch event, ArkUI performs hit testing on the touch points and component area before the touch event is triggered, in order to determine the component that will respond to the touch event, and then dispatches the touch event based on the hit test result. The hitTestBehavior attribute sets how the component behaves during hit testing. The settings will affect the hit test result and thereby the subsequent touch event distribution. For details, see HitTestMode.

NOTE - The APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version. - When the touch areas of nodes in the Stack component overlap, hit testing is performed only on the node displayed at the top layer by default. To perform hit testing on the node at the lower layer, set hitTestBehavior to HitTestMode.Transparent for the upper-layer node.

hitTestBehavior

hitTestBehavior(value: HitTestMode)

Sets how the component behaves during hit testing.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
value HitTestMode Yes How the component behaves during hit testing.
Default value: HitTestMode.Default

HitTestMode

Name Value Description
Default 0 Both the node and its child nodes respond to the hit test of a touch event, but its sibling nodes are blocked from the hit test. The hit test for ancestor nodes is not affected.
Block 1 The node responds to the hit test of a touch event, but its child nodes and sibling nodes are blocked from the hit test. Ancestor nodes are also blocked from the hit test.
Transparent 2 Both the node and its child nodes respond to the hit test of a touch event, and its sibling nodes are also considered during the hit test. The hit test for ancestor nodes is not affected.
None 3 The node does not respond to the hit test of a touch event, but its child node and sibling node are considered during the hit test. The hit test for ancestor nodes is not affected.

Example

This example demonstrates the touch effects of Block and Transparent hit test modes by setting different HitTestMode values.

// xxx.ets
@Entry
@Component
struct HitTestBehaviorExample {
  build() {
    // outer stack
    Stack() {
      Button('outer button')
        .onTouch((event) => {
          console.info('outer button touched type: ' + (event as TouchEvent).type)
        })
      // inner stack
      Stack() {
        Button('inner button')
          .onTouch((event) => {
            console.info('inner button touched type: ' + (event as TouchEvent).type)
          })
      }
      .width("100%").height("100%")
      .hitTestBehavior(HitTestMode.Block)
      .onTouch((event) => {
        console.info('stack touched type: ' + (event as TouchEvent).type)
      })

      Text('Transparent')
        .hitTestBehavior(HitTestMode.Transparent)
        .width("100%").height("100%")
        .onTouch((event) => {
          console.info('text touched type: ' + (event as TouchEvent).type)
        })
    }.width(300).height(300)
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

0  赞