4

我在 tvOS 的 Swift 游戏中添加了一些按钮。

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)
}

比赛开始时。初始焦点设置为 button1。我希望将焦点设置为 button3。我了解首选焦点是根据从屏幕左上角开始找到的第一个可聚焦对象自动选择的。有谁知道如何覆盖它,以便我可以在游戏开始时将 button3 设置为首选焦点视图?

4

2 回答 2

7

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

override var preferredFocusedView: UIView? {
    get {
        return self.button3
    }
}
于 2015-11-16T20:20:58.137 回答
6

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

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

于 2015-09-19T16:50:31.393 回答