zepojo/UPCarouselFlowLayout

Auto-select the center cell?

Opened this issue · 1 comments

As of now, user must tap a cell in order to trigger didSelectItemAtIndexPath. Is there a way to select the center cell once the scrolling ended automatically?

Take a look to the example app: if you implement the scrollViewDidEndDecelerating method of UIScrollViewDelegate you could calculate the number of item/page and therefore, get the cell using it as index.

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let layout = self.collectionView.collectionViewLayout as! UPCarouselFlowLayout
        let pageSide = (layout.scrollDirection == .horizontal) ? self.pageSize.width : self.pageSize.height
        let offset = (layout.scrollDirection == .horizontal) ? scrollView.contentOffset.x : scrollView.contentOffset.y
        currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CarouselCollectionViewCell.identifier, for: IndexPath(row: currentPage, section: 0))
}