我在 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成为首选焦点视图?
我希望这就是您正在寻找的,祝您好运! :)
override var preferredFocusedView: UIView? {
get {
return self.button3
}
}
preferredFocusedView 已弃用,因此请使用下面
@IBOutlet weak var viewTemp: UIView!
override var preferredFocusEnvironments : [UIFocusEnvironment] {
//Condition
return [viewTemp]
}
use override var PreferredFocusEnvironments :[UIFocusEnvironment],是一组焦点环境,按优先级排序,该环境更喜欢在焦点更新期间将焦点定向到该数组。