6 Replies
      Latest reply on Feb 13, 2020 10:22 AM by mr.bodich
      mr.bodich Level 1 Level 1 (0 points)

        UICollectionView don't animate when removing 2nd item of 2 total, just jumps after pre-last item disappear.

         

         

        UICollectionView animates add/remove item fine in all other cases (works fine for deleting 3rd of 3, 4th of 4, 15th of 15 etc) excepting the situation when you try to remove 2nd of 2 item. Have no idea why, does anyone can help me to solve that? Please watch the short video. You can try the ready project as well.

         

         

        ***Custom FlowLayout is must have, no chance to remove it.

         

        Video explanation

        https://vimeo.com/390343515

         

        Sample project on GitHub

        https://github.com/mrbodich/Test-Collectionview

         

         

        Thank you for any help

        • Re: UICollectionView not scrolling (jumping) when removing 2nd of 2 item
          Claude31 Level 8 Level 8 (8,145 points)

          Video does not show a lot. All pages are yellow. What are we supposed to notice ?

            • Re: UICollectionView not scrolling (jumping) when removing 2nd of 2 item
              mr.bodich Level 1 Level 1 (0 points)

              Look at the animation when I am removing cards. When I remove 2nd card (and 2 cards total left!) animation is not smooth. Card is disappear and after that scroll position jumps to the single card left. I've turned on slow animations in the end of the video to make it super easy to notice.

              No matter cards are just yellow, you should look on scroll position and card disappearing animations only.

              Tell me please if you see what I am talking about now.

                • Re: UICollectionView not scrolling (jumping) when removing 2nd of 2 item
                  Claude31 Level 8 Level 8 (8,145 points)

                  OK. That would be easier to see with different colors, but I think I got it.

                  I have adapted code to make it easier to notice

                   

                      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath)
                          switch indexPath.row {
                          case 0: cell.backgroundColor = .blue
                          case 1: cell.backgroundColor = .green
                          case 2: cell.backgroundColor = .yellow
                          case 3: cell.backgroundColor = .orange
                          case 4: cell.backgroundColor = .red
                          case 5: cell.backgroundColor = .purple
                          default: cell.backgroundColor = .yellow
                          }
                          return cell
                      }

                  When you remove 3rd on 3, or 4 on 4, there is a flip effect.

                  Not when you remove the 2nd out of 2.

                   

                  I think it is a 'normal' behavior.

                   

                  When you have 3 cards, and remove 3rd, you can swipe n°2 in place of 3 and n°1 in place of 2 (we see it on screen).

                  But when there are only 2 left, and remove the second you could swipe n°1 in place of 1 but there is nothing to move in place of  n°1.

                  Hence animation cannot start and is replaced by a simple substitution.

                   

                  Could ask support for confirmation if that is really important for you.

                    • Re: UICollectionView not scrolling (jumping) when removing 2nd of 2 item
                      mr.bodich Level 1 Level 1 (0 points)

                      No, actually as I see it, here is how it works. Not sure why you saying it's swiping there (or maybe I misunderstood you).

                       

                      When you remove the last card (and have more than 2 cards total), we have 2 animations at the same time:

                      1. Card alpha from 1 to 0, so it bacame invisible
                      2. collectionView's contentOffset animates to the previous card that is going to be last.

                       

                      When we have 2 cards total and removing the last we have these animations:

                      1. Card alpha from 1 to 0, so it bacame invisible
                      2. collectionView's contentOffset animates to the previous card that is going to be last. — This animation just don't work and when disappearing card disappears entirely, contentOffset just jumps where it should be without animation.

                       

                      It looks like an old Apple's bug. I've seen the same question 5 years old and this behavior was not solved yet.

                      Unfortunately I cannot ask support because I don't have a paid developer account for now :((( I am in the process creating my first serious app. Will buy pro right before sending to AppStore.