How do I create a 'sticky' horizontal scrolling effect with collection view Swift 3

Viewed 934

I have a collection view with multiple cells. Users can scroll horizontality. However, I want it to center the nearest cell once the user lets go. Or, if not, have some form of paging but for individual cells. I'm making basically an icon/profile picture picker.

@IBOutlet weak var profilePicScrollView: UIScrollView!
var profilePicsArray = [#imageLiteral(resourceName: "sample_user_photo"), #imageLiteral(resourceName: "settings_icon"), #imageLiteral(resourceName: "usernametextfield_dark")]

@IBOutlet weak var profilePicCollectionView: UICollectionView!


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return profilePicsArray.count
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionat section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageViewCellReuseID", for: indexPath) as! SettingsCollectionViewCell

    cell.imageInCollection.image =  profilePicsArray[indexPath.row]
    cell.imageInCollection.backgroundColor = UIColor.getRandomColor()

    return cell
}
1 Answers
Related