tvos 提升集合视图单元格在焦点上

tvos elevate collection view cell on focus

我想增加集中在集合视图中的 UICollectionViewCell 的大小。

如何在聚焦时增加 UICollectionViewCell 的大小?我不能用 CGAffine 做到这一点,我试过这个:

cellChosen.transform = CGAffineTransform(scaleX: 2, y: 2)

但是没用。我做错了吗?

UIImageView 上还有一个名为 adjustImageWhenAncestorFocused 的效果,非常酷。可以用在手机上吗?

重要提示:我使用的是 swift 3,单元格有一个 UILabel 和一个 UIImageView。

您需要在 didUpdateFocusInContext 中使用转换。例如

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

添加避免焦点集合视图的集合视图委托

func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

在 UICollectionViewCell

中覆盖 UIFocusEnvironment
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

    coordinator.addCoordinatedAnimations({

        if self.focused 
        {
          // Apply focused appearence
        } 
        else 
        {
          // Apply normal appearance
        }


    }, completion: nil)
}

过渡。在 UICollectionViewCell

上添加这些行
override func awakeFromNib() {
    super.awakeFromNib()

    imageView.adjustsImageWhenAncestorFocused = true
    imageView.clipsToBounds = false
    label.alpha = 0.0
}