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

3

我希望能够在集合视图中放大一个聚焦的UICollectionViewCell。

当UICollectionViewCell处于聚焦状态时,如何增加其大小?我尝试使用CGAffine无法实现,我尝试了以下方法:

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

但是它没有奏效。我做错了吗?

UIImageView还有一个很酷的效果叫做adjustImageWhenAncestorFocused。能不能在cell上使用它?

重要提示:我正在使用Swift 3,该单元格具有UILabel和UIImageView。

2个回答

4

您需要在didUpdateFocusInContext中使用transform。例如:

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

3
添加集合视图委托,以避免焦点集合视图。
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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接