微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 如何在按钮焦点上执行tvOS中的动作事件

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

我有四个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 @R_556_4045@ion 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
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐