For the complete documentation index, see llms.txt. This page is also available as Markdown.

曝光检测

可以检测滚动视图或列表中某个元素是否出现在屏幕上。

检测可见性

SDK 组件: InView

InView 该组件用于检测元素何时开始出现在屏幕上或消失。只要元素开始在屏幕上露出一点, onChanged 处理函数就会被调用,并且第一个参数会传入 true 。相反,当元素从屏幕上消失时, false 值会传入。 onChanged 处理函数的第二个参数会传入元素的屏幕可见比例。可见比例值在 01.0 之间。例如, 0.2被传入时,表示该组件有 20% 显示在屏幕上。

请注意

InView必须 IOContext包含的 IOScrollViewIOFlatList 内部使用。如果在 IOContext 外部使用, IOProviderMissingError会发生错误。

签名

class InView<T = ViewProps> extends PureComponent<InViewProps<T>> {
  static contextType: import('react').Context<IOContextValue>;
  static defaultProps: Partial<InViewProps>;
  context: undefined | IOContextValue;
  mounted: boolean;
  protected element: Element;
  protected instance: undefined | ObserverInstance;
  protected view: any;
  constructor(props: InViewProps<T>);
  componentDidMount(): void;
  componentWillUnmount(): void;
  protected handleChange: (inView: boolean, areaThreshold: number) => void;
  protected handleRef: (ref: any) => void;
  protected handleLayout: (event: LayoutChangeEvent) => void;
  measure: (...args: any) => void;
  measureInWindow: (...args: any) => void;
  measureLayout: (...args: any) => void;
  setNativeProps: (...args: any) => void;
  focus: (...args: any) => void;
  blur: (...args: any) => void;
  render(): import('react/jsx-runtime').JSX.Element | null;
}

参数

  • props · 必需 · Object

    传递给组件的 props 对象。

    • props.children · 必需 · React.ReactNode

      将在组件下方渲染的子组件。

  • prop.asReact.ComponentType · View

    指定要实际渲染的组件。默认值是 View 组件。

  • triggerOnceboolean · false

    当元素第一次出现在屏幕上时,只想调用一次 onChange 回调时使用此选项。

  • onLayout(event: LayoutChangeEvent) => void

    布局发生变化时调用的回调函数。

  • onChange(inView: boolean, areaThreshold: number) => void

    当元素出现在屏幕上或消失时调用的回调函数。第一个参数传入可见状态,第二个参数传入可见比例。

示例


检测列表可见性

SDK 组件: IOFlatList

IOFlatList是添加了 Intersection Observer 功能,用于检测滚动过程中某个元素是否出现在屏幕上或消失的 FlatList 组件。使用该组件可以轻松确认并处理列表中每一项是否出现在屏幕上。

InView一起使用时,可以确认各元素的可见状态。作为子元素包含的 InView 组件会 IOFlatList通过 的观察功能检测元素是否出现在屏幕上,并根据可见状态触发事件。

签名

示例


检测滚动区域可见性

SDK 组件: IOScrollView, ImpressionArea

IOScrollViewImpressionArea结合使用,可以确认滚动视图中元素是否出现在屏幕上。某个元素在屏幕上显示达到一定比例以上时, onImpressionStart 回调会被调用。

ImpressionAreaareaThreshold 值后,当元素可见比例达到设定比例以上时, onImpressionStart 回调会被调用。

IOScrollView 只能在内部使用

ImpressionArea必须 IOScrollView 内部。

否则, 在 IOContext.Provider 外部使用了。会发生这样的错误。

在滚动视图中处理元素显示达到 20% 以上时

下面的代码是元素高度 100px的元素在 IOScrollView20%以上显示时onImpressionStart会被调用的示例。

红色线条是 100px20% 位置的可视化标示示例。

最后更新于

这有帮助吗?