Skip to content
Browse files

Better match scroll to item behavior to UICollectionView

  • Loading branch information...
1 parent e0c2e8c commit 885d275d8016b3062fd8260ade9af59e8fac380e @steipete committed Mar 20, 2013
Showing with 4 additions and 8 deletions.
  1. +4 −8 PSTCollectionView/PSTCollectionView.m
View
12 PSTCollectionView/PSTCollectionView.m
@@ -647,22 +647,19 @@ - (NSArray *)indexPathsForSelectedItems {
// Interacting with the collection view.
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(PSTCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
-
- // ensure grid is laid out; else we can't scroll.
+ // Ensure grid is laid out; else we can't scroll.
[self layoutSubviews];
PSTCollectionViewLayoutAttributes *layoutAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:indexPath];
-
if (layoutAttributes) {
CGRect targetRect = [self makeRect:layoutAttributes.frame toScrollPosition:scrollPosition];
-
[self scrollRectToVisible:targetRect animated:animated];
}
}
- (CGRect)makeRect:(CGRect)targetRect toScrollPosition:(PSTCollectionViewScrollPosition)scrollPosition {
// split parameters
- NSUInteger verticalPosition = scrollPosition & 0x07; // 0000 0111
+ NSUInteger verticalPosition = scrollPosition & 0x07; // 0000 0111
NSUInteger horizontalPosition = scrollPosition & 0x38; // 0011 1000
if (verticalPosition != PSTCollectionViewScrollPositionNone
@@ -681,21 +678,20 @@ - (CGRect)makeRect:(CGRect)targetRect toScrollPosition:(PSTCollectionViewScrollP
}
CGRect frame = self.layer.bounds;
-
CGFloat calculateX;
CGFloat calculateY;
switch(verticalPosition) {
case PSTCollectionViewScrollPositionCenteredVertically:
- calculateY = targetRect.origin.y-((frame.size.height/2)-(targetRect.size.height/2));
+ calculateY = fmaxf(targetRect.origin.y-((frame.size.height/2)-(targetRect.size.height/2)), -self.contentInset.top);
targetRect = CGRectMake(targetRect.origin.x, calculateY, targetRect.size.width, frame.size.height);
break;
case PSTCollectionViewScrollPositionTop:
targetRect = CGRectMake(targetRect.origin.x, targetRect.origin.y, targetRect.size.width, frame.size.height);
break;
case PSTCollectionViewScrollPositionBottom:
- calculateY = targetRect.origin.y-(frame.size.height-targetRect.size.height);
+ calculateY = fmaxf(targetRect.origin.y-(frame.size.height-targetRect.size.height), -self.contentInset.top);
targetRect = CGRectMake(targetRect.origin.x, calculateY, targetRect.size.width, frame.size.height);
break;
}

0 comments on commit 885d275

Please sign in to comment.
Something went wrong with that request. Please try again.