【问题标题】:How to make a UIView focusable using the focus engine on Apple TV如何使用 Apple TV 上的焦点引擎使 UIView 可聚焦
【发布时间】:2015-10-11 23:06:20
【问题描述】:

是否可以使UIView 成为焦点?还是应该为所有可能的视图使用自定义 UIButton

我试图覆盖canBecomeFocused,但什么也没发生。

【问题讨论】:

  • 是的,任何 UIView 都可以使用 canBecomeFocused 获得焦点。但是,您还需要重写 didUpdateFocusInContext 方法来显示焦点。

标签: swift tvos apple-tv focus-engine


【解决方案1】:

所以问题是我没有注意到我的单元格获得了焦点。总结一下,你需要实现

1) 覆盖 canBecomeFocused

2) 覆盖“didUpdateFocusInContext:withAnimationCoordinator:”方法,以便能够将单元格突出显示为焦点

Swift 2.3:

override func canBecomeFocused() -> Bool {
    return true
}

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

    if context.nextFocusedView == self {
        coordinator.addCoordinatedAnimations({ () -> Void in
            self.layer.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.2).CGColor
        }, completion: nil)
    } else if context.previouslyFocusedView == self {
        coordinator.addCoordinatedAnimations({ () -> Void in
            self.layer.backgroundColor = UIColor.clearColor().CGColor
        }, completion: nil)
    }
}

斯威夫特 3:

override var canBecomeFocused: Bool {
    return true
}

override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    if context.nextFocusedView == self {
        coordinator.addCoordinatedAnimations({ () -> Void in
            self.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.2).cgColor
        }, completion: nil)

    } else if context.previouslyFocusedView == self {
        coordinator.addCoordinatedAnimations({ () -> Void in
            self.layer.backgroundColor = UIColor.clear.cgColor
        }, completion: nil)
    }
}

【讨论】:

    【解决方案2】:

    似乎事情已经发生了变化,现在使视图具有焦点的正确方法是:

    override var canBecomeFocused : Bool { return true }

    【讨论】:

      【解决方案3】:

      Swift 3 版本的 Pavel 代码:

      override var canBecomeFocused: Bool {
          return true
      }
      
      override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
      
          if context.nextFocusedView == self {
              coordinator.addCoordinatedAnimations({ () -> Void in
                  self.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.2).cgColor
              }, completion: nil)
      
          } else if context.previouslyFocusedView == self {
              coordinator.addCoordinatedAnimations({ () -> Void in
                  self.layer.backgroundColor = UIColor.clear.cgColor
              }, completion: nil)
          }
      }
      

      【讨论】:

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