How to make a UIView focusable using the focus engine on Apple TV
Asked Answered
A

3

7

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

I tried to override canBecomeFocused but nothing happened.

Arenaceous answered 11/10, 2015 at 23:6 Comment(2)
yes, any UIView is focusable using canBecomeFocused. However, you also need to override the didUpdateFocusInContext method to display the focus.Cowbird
you MUST also add isUserInteractionEnabled = trueElboa
A
18

So the problem was that I didn't notice that my cell got focus. To wrap this up, you need to implement

1) override canBecomeFocused

2) override "didUpdateFocusInContext:withAnimationCoordinator:" method to be able to highlight 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)
    }
}
Arenaceous answered 12/10, 2015 at 18:16 Comment(1)
must also have isUserInteractionEnabled = trueElboa
H
3

It seems that things had changed and now the correct way to make a view focusable is this:

override var canBecomeFocused : Bool { return true }

Hustler answered 24/11, 2016 at 10:50 Comment(0)
Z
0

Swift 3 version of Pavel'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)
    }
}
Zepeda answered 26/2, 2017 at 2:38 Comment(1)
isUserInteractionEnabled = trueElboa

© 2022 - 2024 — McMap. All rights reserved.