如何在 tvOS 中将焦点从一个按钮移动到另一个按钮? [英] How to move the focus from a button to another in tvOS?
问题描述
我是 tvOS 的新手.我想要一个标准按钮,一旦按下,它就会将焦点移到另一个标准按钮上,我该怎么做(当然,如果可能的话)?
I'm new to tvOS. I would like to have a standard button that once is pressed, it moves the focus to another standard button, how can I do this (if it's possible of course)?
推荐答案
首先使用自定义属性覆盖 viewController 中的 preferredFocusedView
:
Start by overriding the preferredFocusedView
in your viewController with a custom property:
var myPreferredFocusedView:UIView?
override var preferredFocusedView: UIView? {
return myPreferredFocusedView:UIView
}
然后,在您的按钮回调中,将 myPreferredFocusedView
设置为下一个应该获得焦点的首选按钮.之后,直接请求焦点更新:
Then, in your button callback, set the myPreferredFocusedView
to your next preferred button which should get focus. After that, directly request a focus update:
func buttonCallback(){
myPreferredFocusedView = button2 // button2 is an example
/*
Trigger the focus system to re-query the view hierarchy for preferred
focused views.
*/
setNeedsFocusUpdate()
updateFocusIfNeeded()
}
这应该会将焦点系统更新到您的另一个按钮.
This should update the focus system to your other button.
这篇关于如何在 tvOS 中将焦点从一个按钮移动到另一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!