Sprite kit 如何在tvOS(AppleTV)上运行的视图控制器中的按钮上设置初始焦点?
我在tvOS的快速游戏中添加了一些按钮Sprite kit 如何在tvOS(AppleTV)上运行的视图控制器中的按钮上设置初始焦点?,sprite-kit,swift2,apple-tv,tvos,Sprite Kit,Swift2,Apple Tv,Tvos,我在tvOS的快速游戏中添加了一些按钮 override func didMoveToView(view: SKView) { let button1=UIButton(frame: CGRectMake(330, 800, 300, 100)) button1(imageBuySelected, forState: UIControlState.Focused) button1(imageBuyNormal, forState: UIControlState.Norm
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。我希望焦点设置为按钮3。我知道首选焦点是根据从屏幕左上角开始找到的第一个可聚焦对象自动选择的。有人知道如何覆盖此选项,以便我可以在游戏开始时将button3设置为preferredfocusview吗?在视图或视图控制器中,覆盖
preferredFocusedView
方法以返回button3
您可以在文档部分阅读有关此方法的更多信息。我希望这就是您想要的,祝您好运!:)
我也有类似的问题,但我的按钮是在服务器响应后动态创建的,因此我无法设置首选的FocusedView?您是否尝试过setNeedsFocusUpdate?谢谢你的回复。我已经阅读了文档,并且“某种程度上”理解了需要做什么,但执行失败。有没有可能有人会发布一些示例代码来说明需要做什么?My view controller中的My preferredFocusedView会返回一个不同的按钮,具体取决于启用了哪些按钮。但是preferredFocusedView只被调用了一次,所以我试着在按钮上调用setNeedsFocusUpdate,但这不起作用。您需要在视图控制器上调用它。现在很好。
override var preferredFocusedView: UIView? {
get {
return self.button3
}
}