検索
ホームページウェブフロントエンドjsチュートリアルReact Native でのフォーカス管理の方法

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

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

ays Of Managing Focus In React Native

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

<tvfocusguideview trapfocusup></tvfocusguideview>

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

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

ays Of Managing Focus In React Native

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

 <tvfocusguideview autofocus></tvfocusguideview>

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></view>
<pressable hastvpreferredfocus></pressable>
<touchablehighlight hastvpreferredfocus></touchablehighlight>
<touchableopacity hastvpreferredfocus></touchableopacity>
<textinput hastvpreferredfocus></textinput>
<button hastvpreferredfocus></button>
<tvfocusguideview hastvpreferredfocus></tvfocusguideview>
<touchablenativefeedback hastvpreferredfocus></touchablenativefeedback>
<tvtextscrollview hastvpreferredfocus></tvtextscrollview>
<touchablewithoutfeedback hastvpreferredfocus></touchablewithoutfeedback>

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 までご連絡ください。
JavaScriptの文字列文字を交換しますJavaScriptの文字列文字を交換しますMar 11, 2025 am 12:07 AM

JavaScript文字列置換法とFAQの詳細な説明 この記事では、javaScriptの文字列文字を置き換える2つの方法について説明します:内部JavaScriptコードとWebページの内部HTML。 JavaScriptコード内の文字列を交換します 最も直接的な方法は、置換()メソッドを使用することです。 str = str.replace( "find"、 "置換"); この方法は、最初の一致のみを置き換えます。すべての一致を置き換えるには、正規表現を使用して、グローバルフラグGを追加します。 str = str.replace(/fi

カスタムGoogle検索APIセットアップチュートリアルカスタムGoogle検索APIセットアップチュートリアルMar 04, 2025 am 01:06 AM

このチュートリアルでは、カスタムGoogle検索APIをブログまたはWebサイトに統合する方法を示し、標準のWordPressテーマ検索関数よりも洗練された検索エクスペリエンスを提供します。 驚くほど簡単です!検索をyに制限することができます

独自のAjax Webアプリケーションを構築します独自のAjax Webアプリケーションを構築しますMar 09, 2025 am 12:11 AM

それで、あなたはここで、Ajaxと呼ばれるこのことについてすべてを学ぶ準備ができています。しかし、それは正確には何ですか? Ajaxという用語は、動的でインタラクティブなWebコンテンツを作成するために使用されるテクノロジーのゆるいグループ化を指します。 Ajaxという用語は、もともとJesse Jによって造られました

例JSONファイルの例例JSONファイルの例Mar 03, 2025 am 12:35 AM

この記事シリーズは、2017年半ばに最新の情報と新鮮な例で書き直されました。 このJSONの例では、JSON形式を使用してファイルに単純な値を保存する方法について説明します。 キー価値ペア表記を使用して、あらゆる種類を保存できます

8見事なjQueryページレイアウトプラグイン8見事なjQueryページレイアウトプラグインMar 06, 2025 am 12:48 AM

楽なWebページレイアウトのためにjQueryを活用する:8本質的なプラグイン jQueryは、Webページのレイアウトを大幅に簡素化します。 この記事では、プロセスを合理化する8つの強力なjQueryプラグイン、特に手動のウェブサイトの作成に役立ちます

&#x27; this&#x27; JavaScriptで?&#x27; this&#x27; JavaScriptで?Mar 04, 2025 am 01:15 AM

コアポイント これは通常、メソッドを「所有」するオブジェクトを指しますが、関数がどのように呼び出されるかに依存します。 現在のオブジェクトがない場合、これはグローバルオブジェクトを指します。 Webブラウザでは、ウィンドウで表されます。 関数を呼び出すと、これはグローバルオブジェクトを維持しますが、オブジェクトコンストラクターまたはそのメソッドを呼び出すとき、これはオブジェクトのインスタンスを指します。 call()、apply()、bind()などのメソッドを使用して、このコンテキストを変更できます。これらのメソッドは、与えられたこの値とパラメーターを使用して関数を呼び出します。 JavaScriptは優れたプログラミング言語です。数年前、この文はそうでした

ソースビューアーでjQueryの知識を向上させますソースビューアーでjQueryの知識を向上させますMar 05, 2025 am 12:54 AM

jQueryは素晴らしいJavaScriptフレームワークです。ただし、他のライブラリと同様に、何が起こっているのかを発見するためにフードの下に入る必要がある場合があります。おそらく、バグをトレースしているか、jQueryが特定のUIをどのように達成するかに興味があるからです

モバイル開発用のモバイルチートシート10個モバイル開発用のモバイルチートシート10個Mar 05, 2025 am 12:43 AM

この投稿は、Android、BlackBerry、およびiPhoneアプリ開発用の有用なチートシート、リファレンスガイド、クイックレシピ、コードスニペットをコンパイルします。 開発者がいないべきではありません! タッチジェスチャーリファレンスガイド(PDF) Desigの貴重なリソース

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール