Skip to content

Instantly share code, notes, and snippets.

@celian-m
Created December 8, 2016 15:40
Show Gist options
  • Save celian-m/b2b11866f89cdc6ff08486d9c76d0aa8 to your computer and use it in GitHub Desktop.
Save celian-m/b2b11866f89cdc6ff08486d9c76d0aa8 to your computer and use it in GitHub Desktop.
CustomLayout for CollectionViewLayout
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