組件可見區(qū)域變化事件

2024-01-22 11:24 更新

組件可見區(qū)域變化事件是組件在屏幕中的顯示區(qū)域面積變化時(shí)觸發(fā)的事件,提供了判斷組件是否完全或部分顯示在屏幕中的能力,適用于廣告曝光埋點(diǎn)之類的場(chǎng)景。

說(shuō)明

從API Version 9開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

事件

名稱

功能描述

onVisibleAreaChange(ratios: Array<number>, event: (isVisible: boolean, currentRatio: number) => void)

組件可見區(qū)域變化時(shí)觸發(fā)該回調(diào)。

-ratios:閾值數(shù)組。其中,每個(gè)閾值代表組件可見面積(即組件在屏幕顯示區(qū)的面積)與組件自身面積的比值。當(dāng)組件可見面積與自身面積的比值大于或小于閾值時(shí),均會(huì)觸發(fā)該回調(diào)。每個(gè)閾值的取值范圍為[0.0, 1.0],如果開發(fā)者設(shè)置的閾值超出該范圍,則會(huì)實(shí)際取值0.0或1.0.

-isVisible:表示組件的可見面積與自身面積的比值是否大于閾值,true表示大于,false表示小于。

-currentRatio:觸發(fā)回調(diào)時(shí),組件可見面積與自身面積的比值。

說(shuō)明:

該接口只適用于組件布局區(qū)域超出或離開了當(dāng)前屏幕顯示區(qū)域的情況,不支持組件堆疊(Stack)導(dǎo)致的面積不可見、使用offset或translate等圖形變換接口導(dǎo)致的面積超出情況。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct ScrollExample {
  5. scroller: Scroller = new Scroller()
  6. private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  7. @State testTextStr: string = 'test'
  8. @State testRowStr: string = 'test'
  9. build() {
  10. Column() {
  11. Column() {
  12. Text(this.testTextStr)
  13. .fontSize(20)
  14. Text(this.testRowStr)
  15. .fontSize(20)
  16. }
  17. .height(100)
  18. .backgroundColor(Color.Gray)
  19. .opacity(0.3)
  20. Scroll(this.scroller) {
  21. Column() {
  22. Text("Test Text Visible Change")
  23. .fontSize(20)
  24. .height(200)
  25. .margin({ top: 50, bottom: 20 })
  26. .backgroundColor(Color.Green)
  27. // 通過設(shè)置ratios為[0.0, 1.0],實(shí)現(xiàn)當(dāng)組件完全顯示或完全消失在屏幕中時(shí)觸發(fā)回調(diào)
  28. .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
  29. console.info('Test Text isVisible: ' + isVisible + ', currentRatio:' + currentRatio)
  30. if (isVisible && currentRatio >= 1.0) {
  31. console.info('Test Text is fully visible. currentRatio:' + currentRatio)
  32. this.testTextStr = 'Test Text is fully visible'
  33. }
  34. if (!isVisible && currentRatio <= 0.0) {
  35. console.info('Test Text is completely invisible.')
  36. this.testTextStr = 'Test Text is completely invisible'
  37. }
  38. })
  39. Row() {
  40. Text('Test Row Visible Change')
  41. .fontSize(20)
  42. .margin({ bottom: 20 })
  43. }
  44. .height(200)
  45. .backgroundColor(Color.Yellow)
  46. .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
  47. console.info('Test Row isVisible:' + isVisible + ', currentRatio:' + currentRatio)
  48. if (isVisible && currentRatio >= 1.0) {
  49. console.info('Test Row is fully visible.')
  50. this.testRowStr = 'Test Row is fully visible'
  51. }
  52. if (!isVisible && currentRatio <= 0.0) {
  53. console.info('Test Row is is completely invisible.')
  54. this.testRowStr = 'Test Row is is completely invisible'
  55. }
  56. })
  57. ForEach(this.arr, (item) => {
  58. Text(item.toString())
  59. .width('90%')
  60. .height(150)
  61. .backgroundColor(0xFFFFFF)
  62. .borderRadius(15)
  63. .fontSize(16)
  64. .textAlign(TextAlign.Center)
  65. .margin({ top: 10 })
  66. }, item => item)
  67. }.width('100%')
  68. }
  69. .backgroundColor(0x317aff)
  70. .scrollable(ScrollDirection.Vertical)
  71. .scrollBar(BarState.On)
  72. .scrollBarColor(Color.Gray)
  73. .scrollBarWidth(10)
  74. .onScroll((xOffset: number, yOffset: number) => {
  75. console.info(xOffset + ' ' + yOffset)
  76. })
  77. .onScrollEdge((side: Edge) => {
  78. console.info('To the edge')
  79. })
  80. .onScrollEnd(() => {
  81. console.info('Scroll Stop')
  82. })
  83. }.width('100%').height('100%').backgroundColor(0xDCDCDC)
  84. }
  85. }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)