Paging Uicollectionview by Cells, Not Screen

Paging UICollectionView by cells, not screen

OK, so I found the solution here: targetContentOffsetForProposedContentOffset:withScrollingVelocity without subclassing UICollectionViewFlowLayout

I should have searched for targetContentOffsetForProposedContentOffset in the begining.

UICollectionView Horizontal Paging not centered

Remove spaces between items. For horizontal scrolling collection view set minimum line spacing to 0. You can do this with interface builder or with method of UICollectionViewDelegateFlowLayout protocol:

- (CGFloat)collectionView:(UICollectionView *)collectionView 
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}

enter image description here

Another way is making your cell's width less than collectionView's width for a value of horizontal space between items. Then add section insets with left and right insets that equal a half of horizontal space between items. For example, your minimum line spacing is 10:

- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(collectionView.frame.size.width - 10, collectionView.frame.size.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 5, 0, 5);
}

enter image description here

And third way: manipulate collectionView scroll in scrollViewDidEndDecelerating: method:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView == self.collectionView) {
CGPoint currentCellOffset = self.collectionView.contentOffset;
currentCellOffset.x += self.collectionView.frame.size.width / 2;
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:currentCellOffset];
[self.collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
}
}

enter image description here

Swift - Paging UICollectionView by cell while keeping the cell horizontally centered

You can use insetForSectionAtIndex to add spacing at first and last cell


UPDATE
You can use scroll view.

First: add leading and trailing to scroll view:

enter image description here

Second: scrollView.clipsToBounds = false
Third: add view to scroll view

func setupScrollView() {
DispatchQueue.main.async {
self.scrollView.layoutIfNeeded() // in order to get correct frame

let numberItem = 6
let spaceBetweenView: CGFloat = 20
let firstAndLastSpace: CGFloat = spaceBetweenView / 2
let width = self.scrollView.frame.size.width
let height = self.scrollView.frame.size.height
let numberOfView: CGFloat = CGFloat(numberItem)
let scrollViewContentSizeWidth = numberOfView * width

self.scrollView.contentSize = CGSize(width: scrollViewContentSizeWidth, height: height)

for index in 0..<numberItem {
let xCoordinate = (CGFloat(index) * (width)) + firstAndLastSpace
let viewFrame = CGRect(x: xCoordinate, y: 0, width: width - spaceBetweenView, height: height)
let view = UIView()
view.backgroundColor = .red

view.frame = viewFrame
self.scrollView.addSubview(view)
}
}
}

UICollectionView horizontal paging with space between pages

Solution one:

  1. collectionView.isPagingEnabled = false
  2. add a minimumLineSpacing for the distance between pages
  3. implement targetContentOffsetForProposedContentOffset:withScrollingVelocity: to move the contentOffset to the closest page. You can calculate the page with simple math based on your itemSize and minimumLineSpacing, but it can take a little work to get it right.

Solution Two:

  1. collectionView.isPagingEnabled = true
  2. add a minimumLineSpacing for the distance between pages
  3. the paging size is based on the bounds of the collectionView. So make the collectionView larger then then screenSize. For example, if you have a minimumLineSpacing of 10 then set the frame of the collectionView to be {0,-5, width+10, height}
  4. set a contentInset equal to the minimumLineSpacing to make the first and last item appear correctly.

Paging with left inset on small cells in UICollectionView

i created a custom collection view layout for paging by cell instead of screen

i also outline a few different ways to customize it with examples in the README

check it out here

let me know if you have any questions about it or need more help

Swift: UICollectionView Paging Enabled

What ever, I can get from your question. I think you want to know on which page it is. So, Here is how i did it:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in colViewSol.visibleCells {
let indexPath = colViewSol.indexPath(for: cell)
// here add the code whatever you want to do
}
}


Related Topics



Leave a reply



Submit