Skip to content

Commit

Permalink
Don't animate when dequeuing cells (#517)
Browse files Browse the repository at this point in the history
https://block.atlassian.net/browse/UI-5081

We now force a non-animated layout before dequeuing a cell, to ensure
that any implicit animations that may occur are avoided. This avoids
issues where you may see an animation as a reused cell's changed state
is applied.
  • Loading branch information
kyleve authored Aug 8, 2024
1 parent 5e406ce commit f4e29a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixed

- Fixed an issue where animations would occur when dequeuing / reusing cells. A layout is now forced without animation before presentation.

### Added

### Removed
Expand Down
14 changes: 14 additions & 0 deletions ListableUI/Sources/ListView/ListView.Delegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ extension ListView
item.willDisplay(cell: cell, in: collectionView, for: indexPath)

self.displayedItems[ObjectIdentifier(cell)] = item

UIView.performWithoutAnimation {
/// Force a layout of the cell before it is displayed, so that any implicit animations
/// are avoided. This ensures that cases like toggling a switch on and off are
/// not animated as the cell comes into view.
cell.layoutIfNeeded()
}
}

func collectionView(
Expand Down Expand Up @@ -175,6 +182,13 @@ extension ListView
headerFooter.collectionViewWillDisplay(view: container)

self.displayedSupplementaryItems[ObjectIdentifier(container)] = headerFooter

UIView.performWithoutAnimation {
/// Force a layout of the cell before it is displayed, so that any implicit animations
/// are avoided. This ensures that cases like toggling a switch on and off are
/// not animated as the cell comes into view.
container.layoutIfNeeded()
}
}

func collectionView(
Expand Down
10 changes: 7 additions & 3 deletions ListableUI/Sources/ListView/ListView.VisibleContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ extension ListView
let removed = self.items.subtracting(newItems)
let added = newItems.subtracting(self.items)

// Note: We set these values _before_ we invoke `setAndPerform`,
// incase `setAndPerform` causes an external callback to trigger
// an update, which could cause this method to be re-entrant.

self.items = newItems
self.headerFooters = newHeaderFooters

removed.forEach {
$0.item.setAndPerform(isDisplayed: false)
}

added.forEach {
$0.item.setAndPerform(isDisplayed: true)
}

self.items = newItems
self.headerFooters = newHeaderFooters

// Inform any state reader callbacks of the changes.

Expand Down

0 comments on commit f4e29a7

Please sign in to comment.