使用多个UICollectionView的图像Scalling

时间:2016-01-07 15:58:56

标签: ios uiimageview uicollectionview tvos nsindexpath

我正在构建一个tvos应用程序。我有一个UITableView,大约有25-30个部分。每个部分只有一行,每行只有一个水平UICollectionView,只有一个部分,但有很多行。在UICollectionView的每一行中,我都有一个图像视图,当我选择UICollectionViewCell时,我会向上扩展。

我知道我可以用adjustsImageWhenAncestorFocused做到这一点,但我的情况稍微复杂一些。实际上我在UICollectionViewCell中有两个imageViews我在较大的imageView上使用adjustsImageWhenAncestorFocused但如果在较小的imageView上使用它,那么我看起来真的很糟糕。所以这就是为什么我必须在较小的imageView上使用缩放。

我面临的问题是,当某个地方UICollectionViewCell indexPath混乱时,这导致当我去一些新的UICollectionViewCell时,较小的图像已经缩放,我变得越来越大,一些由于过度缩放,细胞真的非常大

这是故事板设置的图像

enter image description here

这是我的代码,任何帮助都会很棒。

 func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
        guard let nextIndexPath = context.nextFocusedIndexPath else{
            return
        }


        guard let nextFocusedCell = collectionView.cellForItemAtIndexPath(nextIndexPath) as? MyCollectionViewCell else{
            return

        }

        if nextFocusedCell.teacherImageView != nil {
            coordinator.addCoordinatedAnimations({ () -> Void in
                let focusedTransform = CGAffineTransformScale(nextFocusedCell.teacherImageView.transform, 1.4, 1.4)
                nextFocusedCell.teacherImageView.transform =  focusedTransform
                },
                completion: nil
            )

        }

        guard let previousIndexPath = context.previouslyFocusedIndexPath else {
            return

        }

        guard let prevFocusedCell = collectionView.cellForItemAtIndexPath(previousIndexPath) as? MyCollectionViewCell else{
            return

        }

        if prevFocusedCell.teacherImageView != nil {
            coordinator.addCoordinatedAnimations({ () -> Void in
                let focusedTransform = CGAffineTransformScale(prevFocusedCell.teacherImageView.transform, 1/1.4, 1/1.4)
                prevFocusedCell.teacherImageView.transform =  focusedTransform
                },
                completion: nil
            )

        }


    }

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。实际上,UICollecetionViews的每个部分中此设置中有多个UITableView。功能

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
当我垂直移动时多次调用

意味着当i在图像中的不同行之间移动时,在这种情况下,indexPath context.nextFocusedIndexPathcontext.previouslyFocusedIndexPath处的两个单元格都无效,仅其中一个是有效的,而另一个是nil所以如果其中一个是nil,我就无法返回,我必须检查它们。

类似的情况,当我在一行UITableView部分中进行一次或更精确地移动时,那么在这种情况下

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)

被调用一次,context.nextFocusedIndexPathcontext.previouslyFocusedIndexPath都有效,所以我必须更改它们的CGAffineTransform

以下是可以进一步改进的更新代码。

 func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {


        let nextIndexPath = context.nextFocusedIndexPath
        if let nextIP = nextIndexPath {
            let nextFocusedCell = collectionView.cellForItemAtIndexPath(nextIP) as? MyCollectionViewCell

            if let nextCell = nextFocusedCell {

                if nextCell.teacherImageView != nil {
                    coordinator.addCoordinatedAnimations({ () -> Void in
                        let focusedTransform = CGAffineTransformScale(nextCell.teacherImageView.transform, 1.4, 1.4)
                        nextCell.teacherImageView.transform =  focusedTransform
                        },
                        completion: nil
                    )
                }
            }
        }


        let previousIndexPath = context.previouslyFocusedIndexPath
        if let prevIP = previousIndexPath {
            let previousFocusedCell = collectionView.cellForItemAtIndexPath(prevIP) as? MyCollectionViewCell

            if let previousCell = previousFocusedCell {

                if previousCell.teacherImageView != nil {
                    coordinator.addCoordinatedAnimations({ () -> Void in
                        let focusedTransform = CGAffineTransformScale(previousCell.teacherImageView.transform, 1/1.4, 1/1.4)
                        previousCell.teacherImageView.transform =  focusedTransform
                        },
                        completion: nil
                    )
                }
            }
        }
    }