Last active
August 2, 2018 20:09
-
-
Save voxels/ce00e917533c17ae4202cf2da7ecc429 to your computer and use it in GitHub Desktop.
Transforming a UICollectionViewCell during scrollViewDidScroll with a CGAffineTransform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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