ArkUI在處理觸屏事件時,會在觸屏事件觸發前進行按壓點和組件區域的觸摸測試,來收集需要響應觸屏事件的組件,再基于觸摸測試結果分發相應的觸屏事件。在父節點,開發者可以通過onChildTouchTest決定如何讓子節點去做觸摸測試,影響子組件的觸摸測試,最終影響后續的觸屏事件分發,具體影響參考TouchTestStrategy枚舉說明。
說明:
- 從API Version 11開始支持。后續版本如有新增內容,則采用上角標單獨標記該內容的起始版本。
- onClick以及旋轉、捏合手勢經過自定義事件分發之后可能會因為觸摸熱區沒有命中導致事件不響應。
onChildTouchTest
onChildTouchTest(event: (value: Array<TouchTestInfo>) => TouchResult)
當前組件可通過設置回調來自定義子節點如何去做觸摸測試。
系統能力:?SystemCapability.ArkUI.ArkUI.Full
參數:
參數名 | 類型 | 必填 | 說明 |
---|---|---|---|
value | Array<TouchTestInfo> | 是 | 包含子節點信息的數組。 |
返回值:
類型 | 說明 |
---|---|
TouchTestInfo | 子節點進行觸摸測試的方式。 |
說明:?子節點信息數組中只包含命名節點的信息,即開發者通過id屬性設置了id的節點。
TouchTestInfo說明
名稱 | 類型 | 描述 |
---|---|---|
windowX | number | 按壓點相對于窗口左上角的x軸坐標。 |
windowY | number | 按壓點相對于窗口左上角的y軸坐標。 |
parentX | number | 按壓點相對于父組件左上角的x軸坐標。 |
parentY | number | 按壓點相對于父組件左上角的y軸坐標。 |
x | number | 按壓點相對于子組件左上角的x軸坐標。 |
y | number | 按壓點相對于子組件左上角的y軸坐標。 |
rect | RectResult | 子組件的大小。 |
id | string | 通過id屬性設置的組件id。 |
TouchResult說明
名稱 | 類型 | 必填 | 描述 |
---|---|---|---|
strategy | TouchTestStrategy | 是 | 事件派發策略。 |
id ? | string | 否 | 通過id屬性設置的組件id。 當strategy為TouchTestStrategy.DEFUALT時,id是可選的;當strategy是TouchTestStrategy.FORWARD_COMPEITION或TouchTestStrategy.FORWARD時,id是必需的(如果沒有返回id,則當成TouchTestStrategy.DEFAULT處理)。 |
TouchTestStrategy枚舉說明
名稱 | 描述 |
---|---|
DEFAULT | 自定義分發不產生影響,繼續走組件系統默認分發機制。 |
FORWARD_COMPETITION | 定向派發到指定子節點,同時走ArkUI觸摸測試流程。 |
FORWARD | 定向派發到指定子節點,不走ArkUI觸摸測試流程。 |
示例
示例1
// xxx.ets
import promptAction from '@ohos.promptAction';@Entry
@Component
struct ListExample {private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]@State text: string = 'Button'build() {Column() {List({ space: 12, initialIndex: 0 }) {ForEach(this.arr, (item: number) => {ListItem() {Text('Item ' + item).width('100%').height(56).fontSize(16).textAlign(TextAlign.Start)}.borderRadius(24).backgroundColor(Color.White).padding({ left: 12, right: 12 })}, (item: string) => item)}.listDirection(Axis.Vertical).scrollBar(BarState.Off).edgeEffect(EdgeEffect.Spring).onScrollIndex((start: number, end: number) => {console.info('first' + start)console.info('last' + end)}).onScroll((scrollOffset: number, scrollState: ScrollState) => {console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset)}).width('100%').height('65%').id('MyList')Button(this.text).width(312).height(40).id('Mybutton').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 80 }).onClick(() => {this.text = 'click the button'promptAction.showToast({ message: 'you click the button.', duration: 3000 })})}.width('100%').height('100%').backgroundColor(0xF1F3F5).justifyContent(FlexAlign.End).padding({ left: 12, right: 12, bottom: 24 }).onChildTouchTest((touchinfo) => {for (let info of touchinfo) {if (info.id == 'MyList') {return { id: info.id, strategy: TouchTestStrategy.FORWARD_COMPETITION }}}return { strategy: TouchTestStrategy.DEFAULT }})}
}
點擊下方空白區域后拖動,能夠拖動List滑動;點擊Button按鈕,Button會響應onClick事件。
示例2
// xxx.ets
import promptAction from '@ohos.promptAction';@Entry
@Component
struct ListExample {private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]@State text: string = 'Button'build() {Column() {List({ space: 12, initialIndex: 0 }) {ForEach(this.arr, (item: number) => {ListItem() {Text('Item ' + item).width('100%').height(56).fontSize(16).textAlign(TextAlign.Start)}.borderRadius(24).backgroundColor(Color.White).padding({ left: 12, right: 12 })}, (item: string) => item)}.listDirection(Axis.Vertical).scrollBar(BarState.Off).edgeEffect(EdgeEffect.Spring).onScrollIndex((start: number, end: number) => {console.info('first' + start)console.info('last' + end)}).onScroll((scrollOffset: number, scrollState: ScrollState) => {console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset)}).width('100%').height('65%').id('MyList')Button(this.text).width(312).height(40).id('Mybutton').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 80 }).onClick(() => {this.text = 'click the button'promptAction.showToast({ message: 'you click the button.', duration: 3000 })})}.width('100%').height('100%').backgroundColor(0xF1F3F5).justifyContent(FlexAlign.End).padding({ left: 12, right: 12, bottom: 24 }).onChildTouchTest((touchinfo) => {for (let info of touchinfo) {if (info.id == 'MyList') {return { id: info.id, strategy: TouchTestStrategy.FORWARD }}}return { strategy: TouchTestStrategy.DEFAULT }})}
}
點擊下方空白區域后拖動,能夠拖動List滑動;點擊Button按鈕,Button不會響應onClick事件。
示例3
// xxx.ets
import promptAction from '@ohos.promptAction';@Entry
@Component
struct ListExample {private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]@State text: string = 'Button'build() {Column() {List({ space: 12, initialIndex: 0 }) {ForEach(this.arr, (item: number) => {ListItem() {Text('Item ' + item).width('100%').height(56).fontSize(16).textAlign(TextAlign.Start)}.borderRadius(24).backgroundColor(Color.White).padding({ left: 12, right: 12 })}, (item: string) => item)}.listDirection(Axis.Vertical).scrollBar(BarState.Off).edgeEffect(EdgeEffect.Spring).onScrollIndex((start: number, end: number) => {console.info('first' + start)console.info('last' + end)}).onScroll((scrollOffset: number, scrollState: ScrollState) => {console.info(`onScroll scrollState = ScrollState` + scrollState + `, scrollOffset = ` + scrollOffset)}).width('100%').height('65%').id('MyList')Button(this.text).width(312).height(40).id('Mybutton').fontSize(16).fontWeight(FontWeight.Medium).margin({ top: 80 }).onClick(() => {this.text = 'click the button'promptAction.showToast({ message: 'you click the button.', duration: 3000 })})}.width('100%').height('100%').backgroundColor(0xF1F3F5).justifyContent(FlexAlign.End).padding({ left: 12, right: 12, bottom: 24 }).onChildTouchTest((touchinfo) => {return { strategy: TouchTestStrategy.DEFAULT }})}
}
點擊下方空白區域后拖動,List不會滑動;點擊Button按鈕,Button會響應onClick事件。
最后,有很多小伙伴不知道學習哪些鴻蒙開發技術?不知道需要重點掌握哪些鴻蒙應用開發知識點?而且學習時頻繁踩坑,最終浪費大量時間。所以有一份實用的鴻蒙(Harmony NEXT)資料用來跟著學習是非常有必要的。?
這份鴻蒙(Harmony NEXT)資料包含了鴻蒙開發必掌握的核心知識要點,內容包含了(ArkTS、ArkUI開發組件、Stage模型、多端部署、分布式應用開發、音頻、視頻、WebGL、OpenHarmony多媒體技術、Napi組件、OpenHarmony內核、Harmony南向開發、鴻蒙項目實戰等等)鴻蒙(Harmony NEXT)技術知識點。
希望這一份鴻蒙學習資料能夠給大家帶來幫助,有需要的小伙伴自行領取,限時開源,先到先得~無套路領取!!
?獲取這份完整版高清學習路線,請點擊→純血版全套鴻蒙HarmonyOS學習資料
鴻蒙(Harmony NEXT)最新學習路線
-
?HarmonOS基礎技能
- HarmonOS就業必備技能?
- ?HarmonOS多媒體技術
- 鴻蒙NaPi組件進階
- HarmonOS高級技能
- 初識HarmonOS內核?
- 實戰就業級設備開發
有了路線圖,怎么能沒有學習資料呢,小編也準備了一份聯合鴻蒙官方發布筆記整理收納的一套系統性的鴻蒙(OpenHarmony )學習手冊(共計1236頁)與鴻蒙(OpenHarmony )開發入門教學視頻,內容包含:ArkTS、ArkUI、Web開發、應用模型、資源分類…等知識點。
獲取以上完整版高清學習路線,請點擊→純血版全套鴻蒙HarmonyOS學習資料
《鴻蒙 (OpenHarmony)開發入門教學視頻》
《鴻蒙生態應用開發V2.0白皮書》
《鴻蒙 (OpenHarmony)開發基礎到實戰手冊》
OpenHarmony北向、南向開發環境搭建
?《鴻蒙開發基礎》
- ArkTS語言
- 安裝DevEco Studio
- 運用你的第一個ArkTS應用
- ArkUI聲明式UI開發
- .……
?《鴻蒙開發進階》
- Stage模型入門
- 網絡管理
- 數據管理
- 電話服務
- 分布式應用開發
- 通知與窗口管理
- 多媒體技術
- 安全技能
- 任務管理
- WebGL
- 國際化開發
- 應用測試
- DFX面向未來設計
- 鴻蒙系統移植和裁剪定制
- ……
《鴻蒙進階實戰》
- ArkTS實踐
- UIAbility應用
- 網絡案例
- ……
?獲取以上完整鴻蒙HarmonyOS學習資料,請點擊→純血版全套鴻蒙HarmonyOS學習資料
總結
總的來說,華為鴻蒙不再兼容安卓,對中年程序員來說是一個挑戰,也是一個機會。只有積極應對變化,不斷學習和提升自己,他們才能在這個變革的時代中立于不敗之地。?