Skip to content

Instantly share code, notes, and snippets.

@voxels
Last active August 2, 2018 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voxels/ce00e917533c17ae4202cf2da7ecc429 to your computer and use it in GitHub Desktop.
Save voxels/ce00e917533c17ae4202cf2da7ecc429 to your computer and use it in GitHub Desktop.
Transforming a UICollectionViewCell during scrollViewDidScroll with a CGAffineTransform
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollDirection == .horizontal {
calculateTransforms(with:scrollView.contentOffset)
}
}
func calculateTransforms(with offset:CGPoint) {
guard let collectionView = collectionView else {
return
}
for indexPath in collectionView.indexPathsForVisibleItems {
if let cell = collectionView.cellForItem(at: indexPath) {
let halfWidth = cell.contentView.frame.size.width / 2.0
let realCenter = collectionView.convert(cell.center, to: collectionView.superview)
let diff = abs(halfWidth - realCenter.x)
let scale = 1.0 - diff / 1000.0
let scaleTransform = CGAffineTransform.init(scaleX: scale, y: scale)
cell.transform = scaleTransform
cell.alpha = scale
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment