如何使用 Apple TV 上的焦点引擎使 UIView 可聚焦 [英] How to make a UIView focusable using the focus engine on Apple TV

查看:40
本文介绍了如何使用 Apple TV 上的焦点引擎使 UIView 可聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I tried to override canBecomeFocused but nothing happened.

推荐答案

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

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

1) 覆盖 canBecomeFocused

1) override canBecomeFocused

2) 覆盖 "didUpdateFocusInContext:withAnimationCoordinator:" 方法以能够突出显示单元格为焦点

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

这篇关于如何使用 Apple TV 上的焦点引擎使 UIView 可聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆