Paging UICollectionView by cells, not screen

前端 未结 22 2074
予麋鹿
予麋鹿 2020-12-04 05:00

I have UICollectionView with horizontal scrolling and there are always 2 cells side-by-side per the entire screen. I need the scrolling to stop at the begining

22条回答
  •  渐次进展
    2020-12-04 05:58

    just override the method:

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
        *targetContentOffset = scrollView.contentOffset; // set acceleration to 0.0
        float pageWidth = (float)self.articlesCollectionView.bounds.size.width;
        int minSpace = 10;
    
        int cellToSwipe = (scrollView.contentOffset.x)/(pageWidth + minSpace) + 0.5; // cell width + min spacing for lines
        if (cellToSwipe < 0) {
            cellToSwipe = 0;
        } else if (cellToSwipe >= self.articles.count) {
            cellToSwipe = self.articles.count - 1;
        }
        [self.articlesCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:cellToSwipe inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
    

提交回复
热议问题