Search code examples
tvosfocus-engine

tvos How to let the user move focus more freely?


I am constructing a card-game (for tvos) where the user should be able to remove cards from a board. At the end of the game only a few cards may be left at the board, and they might e.g. be arranged as the buttons in the picture below.

How can I let the user move focus from say button 1 to button 0 or 2?

In un-accepted answers to a similar question at SO, it was suggested that one should use an UIFocusGuide. I do not think that answer is applicable to my problem, because in that approach one must tell the focusGuide the preferredFocusedView in advance (e.g. before the user swipes in siri-remote)! In my case, the preferred next focus depends on the swipe direction, e.g. the focusHeading.

enter image description here


Solution

  • UIFocusGuide is the applicable way to handle this issue, and it is possible to assign each focus guide a preferredFocusedView in advance of knowing which direction the user would move (based on which button has the initial focus).

    For example, here's a solution for your edited example that shows how it's possible to move the focus from focused button 1 to either button 0 or to button 2.

    If the user swipes up or left, the focus would move into one of the green focus guides, which would change the preferred focus view to button 0.

    If the user happened to swipe down or right from button 1, the focus would move into one of the blue focus guides, and button 2 would become focused.

    enter image description here

    As the focus changes, you update the focus guides to guide the focus back in the return direction. For example, once button 0 gained focus, you would update preferredFocusedView for the green focus guides to prefer button 1, so the user could then move down or right from focused button 0 and end up back at button 1.

    enter image description here

    Just keep in mind that you only need a focus guide if the Focus Engine can't determine the next view. You should test different scenarios to determine when a guide would actually be required.