ホームページ  >  記事  >  ウェブフロントエンド  >  React Native でのフォーカス管理の方法

React Native でのフォーカス管理の方法

WBOY
WBOYオリジナル
2024-09-12 10:31:32247ブラウズ

TV アプリ用の React Native でフォーカス管理を処理することになると、開発者はよく知られた 5 つの (悲しみの) 段階を経験することになるかもしれません。 ? ? ? ?

フォーカス管理は、TV プラットフォーム間の断片化によりさまざまなフォーカス管理手法が導入されているため、TV アプリケーション開発における独特の課題です。開発者は、焦点を管理するために複数の戦略を作成して採用することを余儀なくされており、多くの場合、プラットフォーム固有のソリューションとクロスプラットフォームの抽象化を両立させなければなりません。フォーカスの課題は、フォーカスが正しく処理されることを保証するだけでなく、プラットフォームの違いに対処することです。 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 プロジェクトは、Apple TV および Android TV プラットフォームのサポートに特に焦点を当てた、コア React Native フレームワークへの追加および拡張機能を提供するオープンソース パッケージです。このプロジェクトの変更のほとんどは、リモコンの D-Pad を使用した SmartTV でのフォーカスベースのナビゲーションの処理を中心としています。このプロジェクトは (素晴らしい!) Doug Lowder によって管理されており、React Native TV アプリケーションでフォーカス管理を処理する主な方法として一般的に推奨されています。

しかし、多くのコミュニティが管理するプロジェクトと同様に、react-native-tvos プロジェクトは開発者のニーズに基づいて進化しており、現在ではフォーカスを処理する方法が複数あります。追加コンポーネントと、react-native-tvos が提供する既存コンポーネントの拡張機能を見てみましょう。

1.TVフォーカスガイドビュー

TVFocusGuideView は Apple の UIFocusGuide API のサポートを提供し、Android TV と同じ方法で実装されており、他のコントロールと直接並んでいない場合でも、フォーカス可能なコントロールに確実に移動できるようにします -反応ネイティブtvosによると。

たとえば、TVFocusGuideView コンポーネント内でレンダリングされた 10 個の Pressable コンポーネントのグリッドを次に示します。

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 の「宛先」として登録するコンポーネントの配列を設定できます。 例を見てみましょう:

  • destinations プロパティを項目 8 への参照 (destinations={[item8Ref.current]}) に設定すると、最初に TVFocusGuideView に移動したときにフォーカスが項目 8 に移動します。

ays Of Managing Focus In React Native

トラップフォーカスプロップ

<TVFocusGuideView trapFocusUp|trapFocusDown|trapFocusLeft|trapFocusRight  />

この小道具は、指定された方向に対してフォーカスが親コンポーネントから逃げないことを保証します。このプロパティにより、指定された方向に対してフォーカスが親コンポーネントから逃げないようになります。例を見てみましょう:

  • trapFocusLeft プロパティを使用すると、コンテナーの外に左に移動できなくなります

ays Of Managing Focus In React Native

オートフォーカスプロップ

 <TVFocusGuideView autoFocus />

autofocus が true に設定されている場合、TVFocusGuideView はフォーカスを最初のフォーカス可能な子にリダイレクトすることでフォーカスを管理します。また、最後にフォーカスした子を記憶し、その後の訪問時にフォーカスをその子にリダイレクトします。このプロパティが destinations プロパティと一緒に使用される場合、destinations プロパティによって設定されたコンポーネントが優先されます。例を見てみましょう:

  • このプロパティがないと、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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。