I'm trying implement pagination by cell and not by screen in Swift. I found the solution here : targetContentOffsetForProposedContentOffset:withScrollingVelocity without subclassing UICollectionViewFlowLayout
Here is my understanding and conversion to Swift.
My subclass:
class ChannelFlowLayout: UICollectionViewFlowLayout {
func pageWidth () -> CGFloat {
return itemSize.width + minimumLineSpacing
}
func flickVelocity () -> CGFloat {
return 0.3
}
override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
var rawPageValue: CGFloat = collectionView!.contentOffset.x / pageWidth()
var currentPage: CGFloat = (velocity.x > 0.0) ? floor(rawPageValue) : ceil(rawPageValue)
var nextPage: CGFloat = (velocity.x > 0.0) ? ceil(rawPageValue) : floor(rawPageValue)
var pannedLessThanAPage : Bool = fabs(1 + currentPage - rawPageValue) > 0.5
var flicked : Bool = fabs(velocity.x) > flickVelocity()
var targetContentOffset: CGPoint
if pannedLessThanAPage && flicked {
targetContentOffset = CGPoint(x: nextPage * pageWidth(), y: proposedContentOffset.y)
} else {
targetContentOffset = CGPoint(x: round(rawPageValue) * pageWidth(), y: proposedContentOffset.y)
}
return proposedContentOffset
}
}
In my viewController:
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
registerNibs()
var layout = ChannelFlowLayout()
collectionView.pagingEnabled = false
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
collectionView.collectionViewLayout = layout
}
What am I missing? Thanks so much!