Skip to content

Instantly share code, notes, and snippets.

@cbess
Last active August 29, 2015 13:56
Show Gist options
  • Save cbess/8949858 to your computer and use it in GitHub Desktop.
Save cbess/8949858 to your computer and use it in GitHub Desktop.
Adjust the position of a UICollectionViewCell.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*) self.collectionView.collectionViewLayout;
// CGFloat maxOffset = (kItemWidth + flowLayout.minimumInteritemSpacing) * kMaxCount;
NSInteger item = (targetContentOffset->x / (kItemWidth + flowLayout.minimumInteritemSpacing));
NSIndexPath *targetPath = [NSIndexPath indexPathForItem:item inSection:0];
UICollectionViewLayoutAttributes *attrs = [self.collectionView layoutAttributesForItemAtIndexPath:targetPath];
DebugLog(@"target cell attrs: %@", attrs);
// place the offset in front of the cell
targetContentOffset->x = CGRectGetMinX(attrs.frame) - flowLayout.minimumInteritemSpacing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment