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

Incorrect Position of Image when scrollToItem Is Called #2

Open
ghost opened this issue Apr 13, 2019 · 2 comments
Open

Incorrect Position of Image when scrollToItem Is Called #2

ghost opened this issue Apr 13, 2019 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 13, 2019

@MaximKotliar I'm having an issue when I call collectionView.scrollToItem. The image that I am applying parallax too is not in the correct position after I call collectionView.scrollToItem.

This is the code that to scroll:

func refresh() {
    collectionView.reloadData()
    
    let indexPath = IndexPath(item: 0, section: 0)
    collectionView.scrollToItem(at: indexPath, at: [], animated: false)
}

This is the parallax code. I only add parallax to backgroundImageView:

private let parallaxOffsetMultiplier: CGFloat = 75

extension MyCollectionViewCell: Parallaxable {
	func updateHorizontalParallaxOffset(with offset: CGFloat) {
		var targetCenter = contentView.center
		targetCenter.x += offset * parallaxOffsetMultiplier
		targetCenter.y = backgroundImageView.center.y
		backgroundImageView.center = targetCenter
	}
}

Here is an image to illustrate the issue:
collection

This issue is solved after I "manually" scroll by "swiping" on the screen.

Any suggestions?

@ghost ghost changed the title Incorrect Position of Image When scrollToItem Is Called Incorrect Position of Image when scrollToItem Is Called Apr 13, 2019
@ghost
Copy link
Author

ghost commented Apr 14, 2019

@MaximKotliar It seems like the way to solve this is to call collectionView.updateParallaxOnVisibleItems() after scrolling. This is a function in your library. Is this ok to do? Please see code below

func refresh() {
    collectionView.reloadData()
    
    let indexPath = IndexPath(item: 0, section: 0)
    collectionView.scrollToItem(at: indexPath, at: [], animated: false)

   DispatchQueue.main.async {
        self.collectionView.updateParallaxOnVisibleItems()
   }
}

@JayceBryce
Copy link

@MaximKotliar Could you please take a look above? Thanks in advance!

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

1 participant