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

Programatically scroll to next item #17

Open
hugocroch opened this issue Aug 4, 2017 · 2 comments
Open

Programatically scroll to next item #17

hugocroch opened this issue Aug 4, 2017 · 2 comments

Comments

@hugocroch
Copy link

Hello,
I am trying to find a way to scroll to the next item programatically. Is there something that I can use to achieve this?

Thanks

@a-voronov
Copy link

a-voronov commented Aug 17, 2017

@hugocroch just set dummyCount to 1, so it doesn't scroll through several screens (with animation) once you try to select an item.
And call selectItem(at:animated:scrollPosition:) with proper indexPath.
Usually you store your data and selected index in some kind of data object or model in case of mvc.

But to fulfill example from this repo for Pattern1ViewController, next code will do the trick:

fileprivate func nextItemIndexPath() -> IndexPath {
    let nextIndex = pageControl.currentPage + 1
    if nextIndex < itemsCount {
        return IndexPath(item: nextIndex, section: 0)
    }
    return IndexPath(item: 0, section: 0)
}

fileprivate func prevItemIndexPath() -> IndexPath {
    let prevIndex = pageControl.currentPage - 1
    if prevIndex >= 0 {
        return IndexPath(item: prevIndex, section: 0)
    }
    return IndexPath(item: itemsCount - 1, section: 0)
}

and then just call this method whenever you need to move to next item

collectionView.selectItem(at: nextItemIndexPath(), animated: true, scrollPosition: .centeredHorizontally)

@krishnakirana
Copy link

I want to do auto scrollable, Above code works but I wanted in reverse order(Right to left scroll)

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

3 participants