加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:33:49 所属栏目:百科 来源:网络整理
导读:我想在我的按钮处于焦点时执行某些操作,而不是在tvOS中手动点击它. 我有四个UIButtons水平排列,当用户只关注4个按钮中的一个时,我将展示一个包含一些信息的UIView. 如何在不需要单击按钮的情况下执行操作? 解决方法 您可以在其中一个按钮成为焦点视图时执行
我想在我的按钮处于焦点时执行某些操作,而不是在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 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
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读