Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to guide tvOS focus items for curved SKNodes

My tvOS app generates a game board using SKNodes that looks like the following:

enter image description here

Each shape, separated by lines, is an SKNode that is focusable (e.g. each colored wedge is composed of 5 SKNodes that gradually diminish in size closer to the center).

My problem is that the focus engine doesn't focus the next focus item (SKNode) that would feel like the logical, most natural next node to focus. This issue is because the focus engine logic is rectangular while my SKNodes are curved. As you can see below, there are inherent problems when trying to figure out the next focusable item when swiping down from the outermost yellow SKNode:

enter image description here

In the example above, the focus engine deducts that the currently focused area is the area within the red-shaded rectangle based on the node's edges. Due to this logic, the focused rectangle overlaps areas that are not part of the currently focused node, including the entire width of the second yellow SKNode. Therefore when swiping downward, the focus engine skips focus to the third (middle) yellow SKNode.

How would one go about solving this focus issue so that focus is more natural both vertically and horizontally for my circular game board of SKNodes without seeming so sporadic? Is this possible? Perhaps with UIFocusGuides?

like image 879
Aaron Avatar asked Sep 12 '25 11:09

Aaron


1 Answers

You have to handle the focus manually. Use the methods listed below to check for the next focused view from the context.

func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool

In this method, you will get the focus heading direction (UIFocusHeading). Intercept the required direction and save your required next focused view in some property. Manually update the focus by calling below methods

setNeedsFocusUpdate()

updateFocusIfNeeded()

This will trigger the below

preferredFocusEnvironments: [UIFocusEnvironment] { get }

In this check for saved instance, and return the same. This will help you handle the focus manually as per your requirements.

like image 126
Ajinkya Avatar answered Sep 14 '25 01:09

Ajinkya