0

I want to animate the top and bottom most cells in a UICollectionView as they enter and leave the screen. I don't just want to animate using scrollview delegates and a one time animation. I want the animations to be progress driven.

Example: Top most cell gradually fades away as cell scrolls farther offscreen.

I have scoured the internet for a way to do this, but the solutions are either way outdated or don't achieve this effect. Thanks in advance.

bdodge
  • 1
  • 1
  • Bonus points if you know of the new iOS 14 api that can track visible cells. I saw it once, but can't find it again... – bdodge Apr 08 '21 at 22:39
  • 2
    What's the issue with using scrollview delegates? The API for getting visibleCells has been there since UICollectionView was created and it's called `visibleCells` – Paul.s Apr 08 '21 at 22:46
  • Also, the scroll delegate will let you know the "percent" of visibility, the content offset/part of the cell visible, to know the alpha to apply to the cell. That's how I'd do it: listen to did scroll. Get visible cells, check if it's partially hidden, check how much, change the alpha in response for each visible cells. – Larme Apr 09 '21 at 08:17

1 Answers1

0

You could try to use NSCollectionLayoutSectionVisibleItemsInvalidationHandler

let section = NSCollectionLayoutSection(group: group)

section.visibleItemsInvalidationHandler = { visibleItems, scrollOffset, layoutEnvironment in
     // perform animations on the visible items
}

If you know height of cells, you could track with scrollOffset when top cells will be scrolled away and add animations.

When a new item is added to visibleItems, you could apply to it animations too. Also if a new item is added, than the top one will be replaced, so you could work just with visibleItems for both cases.

Viktor Gavrilov
  • 830
  • 1
  • 8
  • 13