Created
December 8, 2016 15:40
-
-
Save celian-m/b2b11866f89cdc6ff08486d9c76d0aa8 to your computer and use it in GitHub Desktop.
CustomLayout for CollectionViewLayout
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
class CustomLayout : UICollectionViewFlowLayout { | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
self.itemSize = CGSize(width: itemWidth, height: itemWidth) | |
self.scrollDirection = .horizontal | |
self.minimumLineSpacing = 0 | |
} | |
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | |
let attributes = super.layoutAttributesForElements(in: rect)! | |
let offsetX = self.collectionView!.contentOffset.x | |
for anAttribute in attributes { | |
let x = anAttribute.frame.origin.x - offsetX + halfItemWidth | |
var factor = (1 - abs(x - self.collectionView!.frame.size.width/2)/self.collectionView!.frame.size.width) | |
factor = pow(factor,2) | |
anAttribute.alpha = factor | |
let isLeft : CGFloat = (x - self.collectionView!.frame.size.width/2 ) > 0 ? -1 : 1 | |
let newLayerX = anAttribute.frame.size.width * ( 1 - factor ) / 2 | |
let zoom = CATransform3DMakeScale(factor, factor, 1) | |
let offset = CATransform3DMakeTranslation(newLayerX * isLeft , 0, 0) | |
let transform = CATransform3DConcat(zoom, offset) | |
anAttribute.transform3D = transform | |
} | |
return attributes | |
} | |
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool { | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment