UICollectionView with Paging Enable

前端 未结 7 1481
天命终不由人
天命终不由人 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

    Here's a working swift4 version of Rickster answer:

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {
    
        let cellWidth = 174 as CGFloat
        let cellPadding = 10 as CGFloat
    
        var page = (scrollView.contentOffset.x - cellWidth / 2) / (cellWidth + cellPadding) + 1
    
        if (velocity.x > 0) { page += 1 }
        if (velocity.x < 0) { page -= 1 }
    
        page = max(page,0)
    
        targetContentOffset.pointee.x = page * (cellWidth + cellPadding)
    }
    

提交回复
热议问题