当涉及到在电视应用程序的 React Native 中处理焦点管理时,开发人员可能会发现自己正在经历五个熟悉的阶段(悲伤):? ? ? ? ?
焦点管理是电视应用程序开发中的一个独特挑战,因为电视平台的碎片化导致了各种焦点管理技术的出现。开发人员被迫创建并采用多种策略来管理焦点,通常需要兼顾特定于平台的解决方案和跨平台抽象。焦点的挑战不仅在于确保正确处理焦点,还在于处理平台差异。 Android TV 和 Apple 的 tvOS 具有不同的原生焦点引擎,您可以在我的同事 @hellonehha 撰写的这篇文章中阅读更多相关信息。
最初,特定于 TV 的文档和 API 是主要 React Native 文档的一部分。现在,大多数电视特定内容已转移到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 提供的附加组件和现有组件的增强功能:
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> </> ); };
TVFocusGuideView 接受一些帮助您处理焦点的道具:
<TVFocusGuideView destinations={[]}>
使用 TVFocusGuideView,您可以设置一组组件来注册为 TVFocusGuideView 的“目的地”。 让我们看看我们的例子:
<TVFocusGuideView trapFocusUp|trapFocusDown|trapFocusLeft|trapFocusRight />
此属性可确保焦点不会从给定方向的父组件中逃逸。该属性确保焦点不会从给定方向的父组件中逃逸。让我们看看我们的例子:
<TVFocusGuideView autoFocus />
当自动对焦设置为 true 时,TVFocusGuideView 将通过将焦点重定向到第一个可聚焦的子项来为您管理焦点。它还会记住最后一个关注的孩子,并在后续访问中将焦点重定向到它。如果此属性与目的地属性一起使用,则目的地属性设置的组件将优先。让我们看看我们的例子:
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.
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:
Using the same Pressable and Touchable components from react-native-tvos they accept and execute the onFocus and onBlur props:
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:
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:
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}>
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.
以上是React Native 中管理焦点的方法的详细内容。更多信息请关注PHP中文网其他相关文章!