MiTal Khandhar
MiTal Khandhar

Reputation: 285

How to prevent scrolling left to right of collection view in horizontal scroll direction?

I made a UICollectionView with a horizontal scroll. I want to scroll only one direction i.e right to left my cell view size is as full view. once I scrolling cell, it should not scroll left to right.

Upvotes: 1

Views: 1410

Answers (1)

Aravind A R
Aravind A R

Reputation: 2714

Please Try this,

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let row = scrollView.contentOffset.x / cellWidth
        currentIndexShown = Int(row)

    }

  func scrollViewDidScroll(_ scrollView: UIScrollView) {

        if scrollView.contentOffset.x < cellWidth * CGFloat(currentIndexShown){
            scrollView.contentOffset =  CGPoint(x: cellWidth * CGFloat(currentIndexShown), y: -20)
            scrollView.bounces = false
        } else {
            scrollView.bounces = true
        }
}

Upvotes: 3

Related Questions