Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-select the center cell? #28

Open
rixingw opened this issue Nov 27, 2017 · 1 comment
Open

Auto-select the center cell? #28

rixingw opened this issue Nov 27, 2017 · 1 comment

Comments

@rixingw
Copy link

rixingw commented Nov 27, 2017

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?

@DrankoLQ
Copy link

DrankoLQ commented Mar 9, 2018

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))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants