关注 tvOS 上的 UICollectionViewCells

Focus for UICollectionViewCells on tvOS

我是 tvOS 新手。我在 Objective-C 中使用 XIB 文件创建了 UICollectionView。如何聚焦 UICollectionViewCell?

我在单元格中有一个标签和一个图像视图,我想同时关注标签和图像视图。

UICollectionViewCell默认不是焦点外观,但您可以通过自己添加一个来实现。

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if (self.focused)
    {
        // Apply focused appearence,
        // e.g scale both of them using transform or apply background color 
    }
    else
    {
       // Apply normal appearance
    }
}

如果你只想聚焦ImageView,比如在集合视图单元格获得焦点时放大它,你可以在awakeFromNib方法

中这样做
self.imageView.adjustsImageWhenAncestorFocused = YES;
self.clipToBounds = NO;

collectionview 的自定义 class 中添加:

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) { 
 if (self.focused) {
   collectionView.image.adjustsImageWhenAncestorFocused = true
 } else {
   collectionView.image.adjustsImageWhenAncestorFocused = false
 } 
} 

将以下方法添加到您的 collectionview cell.It 将显示焦点单元格的标题,仅提供焦点的完整外观和感觉。

override func awakeFromNib() {
        super.awakeFromNib()

        // These properties are also exposed in Interface Builder.
        imageView.adjustsImageWhenAncestorFocused = true
        imageView.clipsToBounds = false

        title.alpha = 0.0
    }

    // MARK: UICollectionReusableView

    override func prepareForReuse() {
        super.prepareForReuse()

        // Reset the label's alpha value so it's initially hidden.
        title.alpha = 0.0
    }

    // MARK: UIFocusEnvironment

    override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        /*
         Update the label's alpha value using the `UIFocusAnimationCoordinator`.
         This will ensure all animations run alongside each other when the focus
         changes.
         */
        coordinator.addCoordinatedAnimations({
            if self.focused {
                self.title.alpha = 1.0
            }
            else {
                self.title.alpha = 0.0
            }
            }, completion: nil)
    }