如何使用Apple TV上的焦点引擎使UIView可聚焦

Pav*_*kal 6 apple-tv swift tvos focus-engine

是否有可能UIView成为焦点?或者我应该只UIButton为所有可能的视图使用自定义?

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

Pav*_*kal 15

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

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特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)
    }
}
Run Code Online (Sandbox Code Playgroud)