cogons
7/29/2014 - 6:30 AM

Snaps UICollectionViewCell to center when cell isn't entire width of screen

Snaps UICollectionViewCell to center when cell isn't entire width of screen

//
// Code based on http://markuzweb.blogspot.com.au/2014/02/uicollectionview-paging-for-smaller-width-cells.html
//
// Note, need to create property called "previousXoffset" in class and doesn't need to be 
// initialized by default
//
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                     withVelocity:(CGPoint)velocity
              targetContentOffset:(inout CGPoint *)targetContentOffset {
    
    CGPoint point = *targetContentOffset;
    
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    
    // This assumes that the values of `layout.sectionInset.left` and
    // `layout.sectionInset.right` are the same with `layout.minimumInteritemSpacing`.
    // Remember that we're trying to snap to one item at a time. So one
    // visible item comprises of its width plus the left margin.
    CGFloat visibleWidth = layout.minimumInteritemSpacing + layout.itemSize.width;
    
    // It's either we go forwards or backwards, and only allowing user
    // to scroll either 1 cell left or right (can't scroll more tha 2 cells)
    int indexOfItemToSnap = round(self.previousXoffset / visibleWidth);
    if (self.previousXoffset < point.x) {
        indexOfItemToSnap++;
    } else {
        indexOfItemToSnap--;
    }
    
    // The only exemption is the last item.
    CGFloat xOffset;
    if (indexOfItemToSnap + 1 == [self.collectionView numberOfItemsInSection:0]) { // last item
        xOffset = self.collectionView.contentSize.width - self.collectionView.bounds.size.width;
    } else {
        xOffset = indexOfItemToSnap * visibleWidth;
    }
    
    self.previousXoffset = xOffset;
    *targetContentOffset = CGPointMake(xOffset, 0);
}