swift - TVOS 10 SpriteKit Focus Navigation 默认聚焦项

标签 swift sprite-kit focus tvos

我正在尝试更新我的 SpriteKit 游戏以使用新的 SKNode 焦点导航功能,但我无法更改默认的焦点项目。

基本上我的按钮类中有这段代码来支持焦点导航

class Button: SKSpriteNode {

   var isFocusable = true // easy way to disable focus incase menus are shown etc

   required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        userInteractionEnabled = true
   } 

// MARK: - Focus navigation

#if os(tvOS)
extension Button {

    /// Can become focused
    override var canBecomeFocused: Bool {
        return isFocusable
    }

    /// Did update focus
    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

        if context.previouslyFocusedItem === self {
            // some SKAction to reset the button to default settings  
        }

        else if context.nextFocusedItem === self {
            // some SKAction to scale the button up 
        }
    }
} 
#endif     

一切正常,但默认情况下屏幕左侧的第一个按钮处于焦点状态。

我正在尝试将其更改为另一个按钮,但我做不到。我现在你应该使用

 override var preferredFocusEnvironments: [UIFocusEnvironment]...

 // preferredFocusedView is depreciated

但我不明白如何以及在哪里使用它。

我尝试将这段代码添加到我的菜单场景中,以将焦点按钮从默认(商店按钮)更改为屏幕右侧的播放按钮。

class MenuScene: SKScene {

     // left side of screen
     lazy var shopButton: Button = self.childNode(withName: "shopButton")

    // right side of screen
    lazy var playButton: Button = self.childNode(withName: "playButton")

    // Set preferred focus 
    open override var preferredFocusEnvironments: [UIFocusEnvironment] {
       return [self.playButton]
    }
}

和调用

setNeedsFocusUpdate()
updateFocusIfNeeded()

在 didMoveToView 中,但它不起作用。

如何更改 SpriteKit 中的默认焦点按钮?

最佳答案

多亏了这篇很棒的文章,我终于完成了这项工作。

https://medium.com/folded-plane/tvos-10-getting-started-with-spritekit-and-focus-engine-53d8ef3b34f3#.pfwcw4u70

我完全错过的主要步骤是你告诉你的 GameViewController 你的场景是首选的焦点环境。 这实质上意味着您的 SKScenes 将处理首选焦点而不是 GameViewController。

在 SpriteKit 游戏中,SKScenes 应该使用 SpriteKit API(如 SKLabelNodes、SKSpriteNodes 等)处理 UI,例如按钮。因此,您需要将首选焦点传递给 SKScene。

class GameViewController: UIViewController {

      override func viewDidLoad() {
           super.viewDidLoad()

           // default code to present your 1st SKScene.
      }
}

#if os(tvOS)
extension GameViewController {

      /// Tell GameViewController that the currently presented SKScene should always be the preferred focus environment
      override var preferredFocusEnvironments: [UIFocusEnvironment] {
           if let scene = (view as? SKView)?.scene {
               return [scene]
           } 
           return []
      }
 }
 #endif

您的按钮应该是 SKSpriteNode 的子类,您将在游戏中将其用于所有按钮。使用枚举并给它们不同的名称/标识符以在它们被按下时区分它们(查看 Apple 示例游戏 DemoBots)。

 class Button: SKSpriteNode {

      var isFocusable = true // easy way to later turn off focus for your buttons e.g. when overlaying menus etc.

      /// Can become focused
      override var canBecomeFocused: Bool {
          return isFocusable
      }

      /// Did update focus
      override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

          if context.previouslyFocusedItem === self {
              // SKAction to reset focus animation for unfocused button
          }

          if context.nextFocusedItem === self {
               // SKAction to run focus animation for focused button
          }
      }
 }

在您的开始场景中,您可以将焦点环境设置为您的播放按钮或任何其他按钮。

class StartScene: SKScene {
   ....
}

#if os(tvOS)
extension StartScene {
      override var preferredFocusEnvironments: [UIFocusEnvironment] {
          return [playButton]
     }
}
#endif

如果您在场景中覆盖菜单或其他 UI,您可以执行类似这样的操作,例如 GameScene(如果需要,将焦点移至 gameMenuNode)

class GameScene: SKScene {
   ....
}

#if os(tvOS)
extension GameScene {

      override var preferredFocusEnvironments: [UIFocusEnvironment] {
         if isGameMenuShowing { // add some check like this
             return [gameMenuNode]
         }
         return [] // empty means scene itself
     }    
}
#endif

当您在 SKScenes 之间转换时(例如 StartScene -> GameScene),您还必须告诉您的 GameViewController 更新其焦点环境。如果您使用 SKTransitions,这一点尤其重要,我花了一段时间才弄明白。如果您使用 SKTransitions 而不是新旧场景在转换期间处于事件状态,则 GameViewController 将使用旧场景首选焦点环境而不是新场景,这意味着新场景将无法正确聚焦。

每次在场景之间转换时,我都会这样做。您将不得不稍作延迟,否则将无法正常工作。

 ...
 view?.presentScene(newScene, transition: ...)

 #if os(tvOS)
    newScene.run(SKAction.wait(forDuration: 0.02)) { // wont work without delay
         view?.window?.rootViewController?.setNeedsFocusUpdate()
         view?.window?.rootViewController?.updateFocusIfNeeded()
    }
 #endif

关于swift - TVOS 10 SpriteKit Focus Navigation 默认聚焦项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395167/

相关文章:

swift - “AnyHashable”不能转换为 ‘NSObject’

ios - 在 Swift 中的 View 上添加渐变

swift - 如何使用 swift 以编程方式向 sprite-kit 添加按钮?

swift - 每当我的 TextView 更新时,我的 GameScene 都会重新加载

javascript - 你如何清除javascript中的焦点?

wpf - 当 DataGrid 接收到键盘焦点时,关注 SelectedItem 的 DataGridCell

ios - 在 PDFView (iOS) 上禁用缩放

ios - 带有categoryBitMask的Sprite仍然返回 'nil'

c# - 受按钮影响的按键事件

ios - Swift 文档注释@code 等效。