首页  >  文章  >  web前端  >  React Native 中管理焦点的方法

React Native 中管理焦点的方法

WBOY
WBOY原创
2024-09-12 10:31:32246浏览

当涉及到在电视应用程序的 React Native 中处理焦点管理时,开发人员可能会发现自己正在经历五个熟悉的阶段(悲伤):? ? ? ? ?

焦点管理是电视应用程序开发中的一个独特挑战,因为电视平台的碎片化导致了各种焦点管理技术的出现。开发人员被迫创建并采用多种策略来管理焦点,通常需要兼顾特定于平台的解决方案和跨平台抽象。焦点的挑战不仅在于确保正确处理焦点,还在于处理平台差异。 Android TV 和 Apple 的 tvOS 具有不同的原生焦点引擎,您可以在我的同事 @hellonehha 撰写的这篇文章中阅读更多相关信息。

ays Of Managing Focus In React Native

最初,特定于 TV 的文档和 API 是主要 React Native 文档的一部分。现在,大多数电视特定内容已转移到react-native-tvos项目。

ays Of Managing Focus In React Native

反应本机 tvos

"react-native": "npm:react-native-tvos@latest"

react-native-tvos 项目是一个开源包,为核心 React Native 框架提供补充和扩展,特别关注支持 Apple TV 和 Android TV 平台。该项目中的大部分变化都集中在使用遥控器上的方向键在智能电视上处理基于焦点的导航。该项目由(令人难以置信的!)Doug Lowder 维护,并且通常被推荐作为在 React Native TV 应用程序中处理焦点管理的主要方式。

但是,与许多社区维护的项目一样,react-native-tvos 项目是根据开发人员的需求而发展的,现在有多种方法来处理焦点。让我们探索一下 React-native-tvos 提供的附加组件和现有组件的增强功能:

1.TVFocusGuideView

TVFocusGuideView 提供对 Apple 的 UIFocusGuide API 的支持,并以与 Android TV 相同的方式实现,以帮助确保可以导航到可聚焦控件,即使它们与其他控件不直接对齐 -根据react-native-tvos。

例如,这是在 TVFocusGuideView 组件内呈现的由 10 个可压组件组成的网格:

import { TVFocusGuideView } from 'react-native';

const TVFocusGuideViewExample = () => {
  const [focusedItem, setFocusedItem] = useState(null);

  const renderGridItem = number => (
    <Pressable
      style={[styles.gridItem, focusedItem === number && styles.focusedItem]}
      key={number}
      onFocus={() => setFocusedItem(number)}
      onBlur={() => setFocusedItem(null)}>
      <Text style={styles.gridItemText}>{number}</Text>
    </Pressable>
  );

  return (
    <>
      <Header headerText="Movies" />
      <TVFocusGuideView trapFocusLeft style={styles.grid}>
        {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(num => renderGridItem(num))}
      </TVFocusGuideView>
    </>
  );
};

ays Of Managing Focus In React Native

TVFocusGuideView 接受一些帮助您处理焦点的道具:

目的地道具

<TVFocusGuideView destinations={[]}>

使用 TVFocusGuideView,您可以设置一组组件来注册为 TVFocusGuideView 的“目的地”。 让我们看看我们的例子:

  • 将目标属性设置为对项目 8 的引用 (destinations={[item8Ref.current]}) 会导致当我们最初导航到 TVFocusGuideView 时焦点移动到项目 8。

ays Of Managing Focus In React Native

trapFocus 道具

<TVFocusGuideView trapFocusUp|trapFocusDown|trapFocusLeft|trapFocusRight  />

此属性确保焦点不会从给定方向的父组件中逃逸。该属性确保焦点不会从给定方向的父组件中逃逸。让我们看看我们的例子:

  • 使用 trapFocusLeft 属性,您将无法再从容器中导航到 Left

ays Of Managing Focus In React Native

自动对焦道具

 <TVFocusGuideView autoFocus />

当自动对焦设置为 true 时,TVFocusGuideView 将通过将焦点重定向到第一个可聚焦的子项来为您管理焦点。它还会记住最后一个关注的孩子,并在后续访问中将焦点重定向到它。如果此属性与目的地属性一起使用,则目的地属性设置的组件将优先。让我们看看我们的例子:

  • 如果没有这个道具,当我们从 Header 组件移动到 TVFocusGuideView 时,焦点会转到最近的组件 - 第 3 项(根据 Android 基于邻近度的内置焦点引擎)
  • 使用自动对焦道具,它会转到第 1 项

ays Of Managing Focus In React Native

2. Touchable

With the react-native-tvos, the Touchable component's ( TouchableWithoutFeedback, TouchableHighlight and TouchableOpacity) include additional code to detect focus changes and properly style the components when focused. It also ensures that the appropriate actions are triggered when the user interacts with the Touchable views using the TV remote control.

Specifically, the onFocus event is fired when the Touchable view gains focus, and the onBlur event is fired when the view loses focus. This enables you to apply unique styling or logic when the component is in the focused state that doesn’t come out of the box with core React Native.

Additionally the onPress method has been modified to be triggered when the user selects the Touchable by pressing the "select" button on the TV remote (the center button on the Apple TV remote or the center button on the Android TV D-Pad) and the onLongPress event is executed twice when the "select" button is held down for a certain duration.

3. Pressable

Like Touchable, the Pressable component been enhanced to allow it to accept the onFocus and onBlur props.
Similar to the ‘pressed’ state that is triggered when a user presses the component on a touchscreen, the react-native-tvos Pressable component introduces a focused state that becomes true when the component is focused on the TV screen.

Here’s an example when using the Pressable and Touchable components from React Native core and they do not accept / execute the onFocus and onBlur props:

ays Of Managing Focus In React Native

Using the same Pressable and Touchable components from react-native-tvos they accept and execute the onFocus and onBlur props:

ays Of Managing Focus In React Native

4. hasTVPreferredFocus prop

Some React Native components have the hasTVPreferredFocus prop, which helps you prioritise focus. If set to true, hasTVPreferredFocus will force the focus to that element. According to the React Native docs these are the current components that accept the prop:

ays Of Managing Focus In React Native

However, if you are using react-native-tvOS, there are a lot more components that accept this prop:

<View hasTVPreferredFocus />
<Pressable hasTVPreferredFocus />
<TouchableHighlight hasTVPreferredFocus />
<TouchableOpacity hasTVPreferredFocus />
<TextInput hasTVPreferredFocus />
<Button hasTVPreferredFocus />
<TVFocusGuideView hasTVPreferredFocus />
<TouchableNativeFeedback hasTVPreferredFocus />
<TVTextScrollView hasTVPreferredFocus />
<TouchableWithoutFeedback hasTVPreferredFocus />

Lets look at an example:

  • Setting the hasTVPreferredFocus prop to true for Pressable 2 causes focus be on Pressable 2
  • Changing it to be true when we are on Pressable 3 causes focus to move to Pressable 3

ays Of Managing Focus In React Native

5. nextFocusDirection prop

The nextFocusDirection prop designates the next Component to receive focus when the user navigates in the specified direction helping you handle focus navigation. When using react-native-tvos, this prop is accepted by the same components that accept the hasTVPreferredFocus prop (View, TouchableHighlight, Pressable, TouchableOpacity, TextInput, TVFocusGuideView, TouchableNativeFeedback, Button). Lets look at an example:

nextFocusDown={pressableRef3.current}
nextFocusRight={pressableRef5.current}>
  • Setting the nextFocusDown prop to Pressable 3 causes focus to move to Pressable 3 when the focus moves down
  • Setting the nextFocusRight prop to Pressable 5 causes focus to move to Pressable 5 when the focus moves right

ays Of Managing Focus In React Native

Conclusion

When it comes to handling focus management, there is no one-size-fits-all solution for React Native TV apps. The approach ultimately depends on the specific needs and requirements of your project. While the react-native-tvos provides a useful cross-device abstractions, you may have to adopt platform-specific solutions to handle common fragmentation issues across SmartTV platforms.

Take the time to explore these various focus management solutions so that you can deliver an intuitive focus handling experience for your users, regardless of the SmartTV platform they are using.

Related resources

  • https://dev.to/amazonappdev/tv-navigation-in-react-native-a-guide-to-using-tvfocusguideview-302i
  • https://medium.com/xite-engineering/revolutionizing-focus-management-in-tv-applications-with-react-native-10ba69bd90
  • https://reactnative.dev/docs/0.72/building-for-tv

以上是React Native 中管理焦点的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn