-
-
Save pxpgraphics/c5ff00882caacc5ccfcd to your computer and use it in GitHub Desktop.
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
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity | |
{ | |
CGFloat offSetAdjustment = MAXFLOAT; | |
CGFloat horizontalCenter = (CGFloat) (proposedContentOffset.x + (self.collectionView.bounds.size.width / 2.0)); | |
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height); | |
NSArray *array = [self layoutAttributesForElementsInRect:targetRect]; | |
for (UICollectionViewLayoutAttributes *layoutAttributes in array) | |
{ | |
if(layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) | |
{ | |
CGFloat itemHorizontalCenter = layoutAttributes.center.x; | |
if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offSetAdjustment)) | |
{ | |
offSetAdjustment = itemHorizontalCenter - horizontalCenter; | |
} | |
} | |
} | |
CGFloat nextOffset = proposedContentOffset.x + offSetAdjustment; | |
do { | |
proposedContentOffset.x = nextOffset; | |
CGFloat deltaX = proposedContentOffset.x - self.collectionView.contentOffset.x; | |
CGFloat velX = velocity.x; | |
if(deltaX == 0.0 || velX == 0 || (velX > 0.0 && deltaX > 0.0) || (velX < 0.0 && deltaX < 0.0)) | |
{ | |
break; | |
} | |
if(velocity.x > 0.0) | |
{ | |
nextOffset += [self snapStep]; | |
} | |
else if(velocity.x < 0.0) | |
{ | |
nextOffset -= [self snapStep]; | |
} | |
} while ([self isValidOffset:nextOffset]); | |
proposedContentOffset.y = 0.0; | |
return proposedContentOffset; | |
} | |
- (BOOL)isValidOffset:(CGFloat)offset | |
{ | |
return (offset >= [self minContentOffset] && offset <= [self maxContentOffset]); | |
} | |
- (CGFloat)minContentOffset | |
{ | |
return -self.collectionView.contentInset.left; | |
} | |
- (CGFloat)maxContentOffset | |
{ | |
return [self minContentOffset] + self.collectionView.contentSize.width - self.itemSize.width; | |
} | |
- (CGFloat)snapStep | |
{ | |
return self.itemSize.width + self.minimumLineSpacing; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment