How to make UIView focused with focus on Apple TV

Is it possible to make a UIView focal? Or should I just use a custom UIButton for all possible kinds?

I tried to override canBecomeFocused , but nothing happened.

+6
source share
3 answers

So the problem was that I did not notice that my camera was focused. To wrap this you need to implement

1) override canBecomeFocused

2) override the didUpdateFocusInContext: withAnimationCoordinator: "method to select the cell as focused

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) } } 

Swift 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) } } 
+11
source

It seems that everything has changed, and now the right way to make viewing focused:

override var canBecomeFocused : Bool { return true }

+1
source

Swift 3 versions of Paul's code:

 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) } } 
0
source

All Articles