Tim Nuwin
Tim Nuwin

Reputation: 2887

Enabling paging with a collectionview

I have a collection view that horizontally scrolls 1 cell at a time.

How do I set it so when the paging is enabled, when the cells are at rest you'll still see a little bit of the next cell but there's padding on the left side too?

Upvotes: 0

Views: 115

Answers (1)

Anshad Rasheed
Anshad Rasheed

Reputation: 2566

This is worked for me

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{

    float pageWidth = self.myCollectionView.frame.size.width+8; // width +padding
    float currentOffset = scrollView.contentOffset.x;
    float targetOffset = targetContentOffset->x;
    float newTargetOffset = 0;


      if (targetOffset > currentOffset)
        newTargetOffset = ceilf(currentOffset / pageWidth) * pageWidth;
    else
        newTargetOffset = floorf(currentOffset / pageWidth) * pageWidth;

    if (newTargetOffset < 0)
        newTargetOffset = 0;
    else if (newTargetOffset > scrollView.contentSize.width)
        newTargetOffset = scrollView.contentSize.width;
    targetContentOffset->x = currentOffset;
   [scrollView setContentOffset:CGPointMake(newTargetOffset, 0) animated:YES];
}

you can use uipageviewcontroller also for better results http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/

Upvotes: 1

Related Questions