Vishal-Singh-Panwar / InfiniteScrolling

Add infinite scrolling to collection view.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect Position After scrollToItem(at: indexPath, ...)

ssokol opened this issue · comments

I am attempting to use the horizontal infinite scroll to display compass values (0 - 359). As the compass data is updated, I attempt adjust the display using:

func setCompassHeading(heading: Int) {
    let ip = IndexPath(item: heading, section: 0)
    self.collectionView.scrollToItem(at: ip, at: .centeredHorizontally, animated: true)
}

Values passed are 0 - 359. I expect the collection view to move to the indicated item. However, it is alway off by some number of items. The number of items by which it is off is dependent on the width of the screen. The collection view's width is managed by AutoLayout and adjust depending on if the device (an iPad) is in portrait or landscape mode.

My best guess is that some factor - the width of the items, the spacing between them - is calculated once at load and does not get recalculated after the AutoLayout system has updated the display.

Any suggestions on fixing this? Thanks!