【问题标题】:How to perform an action event in tvOS on button focus如何在 tvOS 中对按钮焦点执行动作事件
【发布时间】:2015-12-25 03:36:43
【问题描述】:

我想在我的按钮处于焦点时执行某些操作,而不是在tvOS 中手动点击它。

我有四个水平排列的 UIButton,当用户只关注 4 个按钮中的一个时,我将显示一个带有一些信息的 UIView。

如何在不点击按钮的情况下执行操作?

【问题讨论】:

    标签: swift uibutton focus tvos tvos9.1


    【解决方案1】:

    当其中一个按钮成为焦点视图时,您可以执行您的操作。

    您可以为每个按钮分配一个标签,并使用焦点按钮的标签来确定要执行的操作。为了便于阅读,您可以为每个标签定义一个 enum 值。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多