4

I just trying to make a custom animation when i scroll to item programmatically. So when I do not compose my animation and using default by cell do not vanishing, but when i put scrolltoItem func inside UIView.animate func the last cell first vanishing and then scrollToItem animates. enter image description here In the second picture in the uppermost collectionView the located before indie game cell firstly disappears and only then collectionView scrolls from indie game cell to the next enter image description here

Why this behavior takes place? Why when i do not animating it purposefully in my way, and just calling scrollToItem with animated = true func, nothing eliminates? if someone do know what happens with the cells, please give me a clue.

  UIView.animate(withDuration: 10, delay: 0, options: .curveEaseInOut, animations: {
                 self.appsCollectionView.scrollToItem(at: IndexPath(item: self.actualNumberOfTheCell, section: 0), at: .centeredHorizontally, animated: false)
            }, completion: nil)
Ninja
  • 309
  • 7
  • 26
  • actually in your code you saying in Animation that I want to scrollTo 4th Item (your current position is 0) so what Animation thinks that you don't need 0th element now so it just removes them and animate your rest of the cells accordingly. You can try to use `setContentOffset` in animation with offset increment of +1 in swipe direction. Don't know about performance it's a try. Just check if it works – Kamaldeep singh Bhatia May 31 '18 at 10:48
  • Still no answer on this? – Liam Bolling Mar 03 '19 at 01:35

3 Answers3

0

I think you should have to do it with self.view.layoutIfNeeded()

self.appsCollectionView.scrollToItem(at: IndexPath(item: self.actualNumberOfTheCell, section: 0), at: .centeredHorizontally, animated: false)
UIView.animate(withDuration: 10, delay: 0, options: .curveEaseInOut, animations: {
          self.view.layoutIfNeeded()   
        }, completion: nil)

Hope it will work

Rakesh Patel
  • 1,673
  • 10
  • 27
  • Yes, but it'll have a very little problem. Just check if your cells are getting reuse or not (print something inside `prepareCellForReuse` inside cell class. If it is reusing it then this is a solution (Y) – Kamaldeep singh Bhatia May 31 '18 at 10:40
0

Your reused cells might be overlapping because of the sudden scroll. The latest cell might have not been dequeued properly and is perhaps initialized using a content of a previous cell -> which is most likely an empty cell that hasn't been properly displayed during the scroll.

Try to reset your cell content using: prepareForReuse() inside your cell's controller.

You should reset your UI to a default state here.

Here's the docs link to it

Don't forget to call a UI display/setup method for your cell, which should display your UI elements.

Hope it helps!

Mert Kahraman
  • 445
  • 6
  • 10
0

I think the issue is caused by collectionView recycling the cell too early, as you are using scrollToItem with animation = false.

I tried to split the animation to several smaller steps, It works, but the scroll is not smooth. Here is the code:

// self is extended from UICollectionView
scrollTo(x: 100, duration: 0.6, count: 4) { (finished) in

        }

 private func scrollTo(x:CGFloat, duration:TimeInterval, count:Int, completion:@escaping (Bool)->Void)
{
    let xOffset = (x - contentOffset.x)/CGFloat(count)
    let durationPart = duration/TimeInterval(count)

    scrollToPart(xOffset: xOffset, duration: durationPart, count: count, completion: completion)
}

private func scrollToPart(xOffset:CGFloat, duration:TimeInterval, count:Int, completion:@escaping (Bool)->Void)
{
    UIView.animate(withDuration: duration, animations: {
        self.contentOffset.x += xOffset
    }) { (finished) in

        if count <= 1
        {
            completion(finished)
        }
        else
        {
            self.scrollToPart(xOffset: xOffset, duration: duration, count: count-1, completion: completion)
        }
    }
}

I used scrollToItem with animated = true at last.

Chen Jiling
  • 519
  • 5
  • 10