【问题标题】:Image Scalling with multiple UICollectionView使用多个 UICollectionView 进行图像缩放
【发布时间】:2016-01-07 15:58:56
【问题描述】:

我正在构建一个 tvos 应用程序。我有一个UITableView,它有大约 25-30 个部分。每个部分只有一行,在每一行中我只有一个水平的UICollectionView,它只有一个部分但有很多行。在@ 987654325中的每一行中,我有一个图像视图,我在选择UICollectionViewCell时缩放。

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

我面临的问题是,当某个地方 UICollectionViewCell indexPath 搞砸了,这导致当我转到一些新的 UICollectionViewCell 时,较小的图像已经被缩放,我变得越来越大,有些单元格真的非常由于过度缩放而非常大

这是故事板设置的图像

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

 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
            )

        }


    }

【问题讨论】:

    标签: ios uiimageview uicollectionview tvos nsindexpath


    【解决方案1】:

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

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

    当我垂直移动时被多次调用意味着当我在图像中的不同行之间移动时,在这种情况下,位于 indexPaths 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
                        )
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 2010-11-29
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多