Skip to content

Instantly share code, notes, and snippets.

@suisho
Created October 9, 2014 13:40
Show Gist options
  • Save suisho/b57b557fb5d98393c1d3 to your computer and use it in GitHub Desktop.
Save suisho/b57b557fb5d98393c1d3 to your computer and use it in GitHub Desktop.
HeaderPagingLayout
class HeaderPagingLayout : UICollectionViewFlowLayout{
let velocityThreshold : CGFloat = 0.1
func proposedAttributes() -> [UICollectionViewLayoutAttributes]{
if let proposedRect = self.collectionView?.bounds{
return self.layoutAttributesForElementsInRect(proposedRect) as? [UICollectionViewLayoutAttributes] ?? []
}
return []
}
func findProposedHeaderAttribute() -> UICollectionViewLayoutAttributes?{
for at in proposedAttributes(){
if (at.representedElementKind ?? "") == UICollectionElementKindSectionHeader{
return at
}
}
return nil
}
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
if let at = findProposedHeaderAttribute(){
let scrollLeft = self.collectionView?.contentOffset.x
if(velocity.x < 0){
return CGPoint.zeroPoint
}
return (scrollLeft > at.center.x) || (velocity.x > velocityThreshold)
? CGPoint(x: at.size.width, y: 0)
: CGPoint.zeroPoint
}
return proposedContentOffset
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment