我想在我的按钮处于焦点时执行某些操作,而不是在tvOS中手动点击它. 我有四个UIButtons水平排列,当用户只关注4个按钮中的一个时,我将展示一个包含一些信息的UIView. 如何在不需要单击按
我有四个UIButtons水平排列,当用户只关注4个按钮中的一个时,我将展示一个包含一些信息的UIView.
如何在不需要单击按钮的情况下执行操作?
您可以在其中一个按钮成为焦点视图时执行操作.您可以为每个按钮分配一个标记,并使用聚焦按钮的标记来确定要采取的操作.为了便于阅读,您可以为每个标记定义一个包含值的枚举.
enum FocusedButtonTag: Int {
case First // Substitute with names that correspond to button's title/action
case Second
case Third
case Fourth
}
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
guard let button = UIScreen.mainScreen().focusedView as? UIButton else {
return
}
// Update your UIView with the desired information based on the focused button
switch button.tag {
case FocusedButtonTag.First.rawValue:
... // first button's action
case FocusedButtonTag.Second.rawValue:
... // second button's action
case FocusedButtonTag.Third.rawValue:
... // third button's action
case FocusedButtonTag.Fourth.rawValue:
... // fourth button's action
default:
break
}
}
