UICollectionView with Paging Enable

前端 未结 7 1473
天命终不由人
天命终不由人 2021-02-06 09:06

I have to create Grid view like Appstore iOS app. I want to do this with UICollectionView paging. I have also implemented the code but not able to scroll like that.

Wha

7条回答
  •  遥遥无期
    2021-02-06 09:31

    I took shtefane's answer and improved on it. Enter your own cellWidth and cellPadding values.

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                         withVelocity:(CGPoint)velocity
                  targetContentOffset:(inout CGPoint *)targetContentOffset
    {
        CGFloat cellWidth = self.cellWidth;
        CGFloat cellPadding = 9;
    
        NSInteger page = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1;
    
        if (velocity.x > 0) page++;
        if (velocity.x < 0) page--;
        page = MAX(page,0);
    
        CGFloat newOffset = page * (cellWidth + cellPadding);
        targetContentOffset->x = newOffset;
    }
    

提交回复
热议问题