使用多个 UICollectionView 进行图像缩放
Image Scalling with multiple UICollectionView
我正在构建一个 tvos 应用程序。我有一个 UITableView
,其中包含大约 25-30 个部分。每个部分只有一行,在每一行中我只有一个水平 UICollectionView
,它只有一个部分但有很多行。 In each row in UICollectionView
i have an image view which i scale up when the UICollectionViewCell
is selected.
我知道我可以用 adjustsImageWhenAncestorFocused
做到这一点,但我的情况稍微复杂一些。实际上,我在 UICollectionViewCell
中有两个 imageView,我在较大的 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
)
}
}
我找到了解决问题的方法。实际上,在 UITableView
的每个部分中,此设置中有多个 UICollecetionViews
。所以函数
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
在我垂直移动时被调用多次意味着当我在图像的不同行之间移动时,在这种情况下,位于 indexPaths context.nextFocusedIndexPath
和 context.previouslyFocusedIndexPath
的两个单元格均无效,其中只有一个是有效的,另一个是 nil
所以我不能 return 如果其中一个是 nil
我必须检查它们。
类似地,当我水平移动一行或更精确地移动一个 UITableView
部分时,那么在那种情况下
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
被调用一次并且 context.nextFocusedIndexPath
和 context.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
)
}
}
}
}
我正在构建一个 tvos 应用程序。我有一个 UITableView
,其中包含大约 25-30 个部分。每个部分只有一行,在每一行中我只有一个水平 UICollectionView
,它只有一个部分但有很多行。 In each row in UICollectionView
i have an image view which i scale up when the UICollectionViewCell
is selected.
我知道我可以用 adjustsImageWhenAncestorFocused
做到这一点,但我的情况稍微复杂一些。实际上,我在 UICollectionViewCell
中有两个 imageView,我在较大的 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
)
}
}
我找到了解决问题的方法。实际上,在 UITableView
的每个部分中,此设置中有多个 UICollecetionViews
。所以函数
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
在我垂直移动时被调用多次意味着当我在图像的不同行之间移动时,在这种情况下,位于 indexPaths context.nextFocusedIndexPath
和 context.previouslyFocusedIndexPath
的两个单元格均无效,其中只有一个是有效的,另一个是 nil
所以我不能 return 如果其中一个是 nil
我必须检查它们。
类似地,当我水平移动一行或更精确地移动一个 UITableView
部分时,那么在那种情况下
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
被调用一次并且 context.nextFocusedIndexPath
和 context.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
)
}
}
}
}