Created
October 9, 2014 13:40
-
-
Save suisho/b57b557fb5d98393c1d3 to your computer and use it in GitHub Desktop.
HeaderPagingLayout
This file contains hidden or 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 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