如何在tvOS(AppleTV)上运行的视图控制器中设置初始焦点?

Dal*_*ard 4 apple-tv sprite-kit swift2 tvos

我在swift游戏中为tvOS添加了一些按钮.

override func didMoveToView(view: SKView) {

    let button1=UIButton(frame: CGRectMake(330, 800, 300, 100))
    button1(imageBuySelected, forState: UIControlState.Focused)
    button1(imageBuyNormal, forState: UIControlState.Normal
    button1 = UIColor.clearColor()
    button1(self, action: "buy1", forControlEvents: UIControlEvents.PrimaryActionTriggered)
    self.view!.addSubview(button1)

    let button2=UIButton(frame: CGRectMake(330, 800, 300, 100))
    button2(imageBuySelected, forState: UIControlState.Focused)
    button2(imageBuyNormal, forState: UIControlState.Normal
    button2 = UIColor.clearColor()
    button2(self, action: "buy2", forControlEvents: UIControlEvents.PrimaryActionTriggered)
    self.view!.addSubview(button2)

    let button3=UIButton(frame: CGRectMake(330, 800, 300, 100))
    button3(imageBuySelected, forState: UIControlState.Focused)
    button3(imageBuyNormal, forState: UIControlState.Normal
    button3 = UIColor.clearColor()
    button3(self, action: "playGame", forControlEvents: UIControlEvents.PrimaryActionTriggered)
    self.view!.addSubview(button3)
}
Run Code Online (Sandbox Code Playgroud)

比赛开始时.初始焦点设置为button1.我希望将焦点设置为button3.我知道首选焦点是根据从屏幕左上角开始找到的第一个可聚焦对象自动选择的.有没有人知道如何覆盖这个,所以我可以在游戏开始时使button3成为preferredfocusview?

Hug*_*ugo 7

我希望这就是你要找的,祝你好运!:)

override var preferredFocusedView: UIView? {
    get {
        return self.button3
    }
}
Run Code Online (Sandbox Code Playgroud)


Jus*_*oss 6

在视图或视图控制器中,覆盖preferredFocusedView要返回的方法button3.

您可以在文档的" 首选焦点链"部分下阅读有关此方法的更多信息.

  • 谢谢回复.我已经阅读了文档,并且"有点"了解需要完成的工作但是没有执行.有人可以发布一些示例代码来说明需要做什么吗? (3认同)