swift – 如何使用Apple TV上的焦点引擎使UIView成为焦点

前端之家收集整理的这篇文章主要介绍了swift – 如何使用Apple TV上的焦点引擎使UIView成为焦点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否可以使UIView成为焦点?或者我应该只为所有可能的视图使用自定义UIButton?

我试图覆盖canBecomeFocused但没有发生任何事情.

所以问题是我没注意到我的细胞得到了关注.要包装它,您需要实现

1)覆盖canBecomeFocused

2)覆盖“didUpdateFocusInContext:withAnimationCoordinator:”方法,以便能够将细胞突出显示为聚焦

Swift 2.3:

  1. override func canBecomeFocused() -> Bool {
  2. return true
  3. }
  4.  
  5. override func didUpdateFocusInContext(context: UIFocusUpdateContext,withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
  6.  
  7. if context.nextFocusedView == self {
  8. coordinator.addCoordinatedAnimations({ () -> Void in
  9. self.layer.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.2).CGColor
  10. },completion: nil)
  11. } else if context.prevIoUslyFocusedView == self {
  12. coordinator.addCoordinatedAnimations({ () -> Void in
  13. self.layer.backgroundColor = UIColor.clearColor().CGColor
  14. },completion: nil)
  15. }
  16. }

斯威夫特3:

  1. override var canBecomeFocused: Bool {
  2. return true
  3. }
  4.  
  5. override func didUpdateFocus(in context: UIFocusUpdateContext,with coordinator: UIFocusAnimationCoordinator) {
  6. if context.nextFocusedView == self {
  7. coordinator.addCoordinatedAnimations({ () -> Void in
  8. self.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.2).cgColor
  9. },completion: nil)
  10.  
  11. } else if context.prevIoUslyFocusedView == self {
  12. coordinator.addCoordinatedAnimations({ () -> Void in
  13. self.layer.backgroundColor = UIColor.clear.cgColor
  14. },completion: nil)
  15. }
  16. }

猜你在找的Swift相关文章