|
1 | 1 | import UIKit |
2 | 2 |
|
3 | | -open class VersatileCell: UICollectionViewCell { |
| 3 | +public struct CellHighlightAnimationContext { |
| 4 | + public let cell: UICollectionViewCell |
| 5 | +} |
| 6 | + |
| 7 | +public protocol CellHighlightAnimation { |
| 8 | + |
| 9 | + @MainActor |
| 10 | + func onChange(isHighlighted: Bool, context: CellHighlightAnimationContext) |
| 11 | +} |
| 12 | + |
| 13 | +public struct DisabledCellHighlightAnimation: CellHighlightAnimation { |
| 14 | + |
| 15 | + public func onChange(isHighlighted: Bool, context: CellHighlightAnimationContext) { |
| 16 | + // no operation |
| 17 | + } |
| 18 | +} |
4 | 19 |
|
5 | | - let animator = UIViewPropertyAnimator(duration: 0.4, dampingRatio: 1) |
| 20 | +public struct ShrinkCellHighlightAnimation: CellHighlightAnimation { |
| 21 | + |
| 22 | + public func onChange(isHighlighted: Bool, context: CellHighlightAnimationContext) { |
| 23 | + |
| 24 | + let animator = UIViewPropertyAnimator(duration: 0.4, dampingRatio: 1) |
| 25 | + |
| 26 | + if isHighlighted { |
| 27 | + animator.addAnimations { |
| 28 | + context.cell.transform = .init(scaleX: 0.95, y: 0.95) |
| 29 | + } |
| 30 | + } else { |
| 31 | + animator.addAnimations { |
| 32 | + context.cell.transform = .identity |
| 33 | + } |
| 34 | + } |
| 35 | + animator.startAnimation() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +extension CellHighlightAnimation { |
| 40 | + |
| 41 | + public static func shrink( |
| 42 | + duration: TimeInterval = 0.4, |
| 43 | + dampingRatio: CGFloat = 1 |
| 44 | + ) -> Self where Self == ShrinkCellHighlightAnimation { |
| 45 | + ShrinkCellHighlightAnimation() |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +extension CellHighlightAnimation where Self == DisabledCellHighlightAnimation { |
| 51 | + public static var disabled: Self { |
| 52 | + DisabledCellHighlightAnimation() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +open class VersatileCell: UICollectionViewCell { |
6 | 57 |
|
7 | 58 | open override var isHighlighted: Bool { |
8 | 59 | didSet { |
9 | 60 | guard oldValue != isHighlighted else { return } |
10 | | - |
11 | | - if isHighlighted { |
12 | | - animator.addAnimations { [self] in |
13 | | - transform = .init(scaleX: 0.95, y: 0.95) |
14 | | - } |
15 | | - } else { |
16 | | - animator.addAnimations { [self] in |
17 | | - transform = .identity |
18 | | - } |
19 | | - } |
20 | | - animator.startAnimation() |
| 61 | + _highlightAnimation.onChange(isHighlighted: isHighlighted, context: .init(cell: self)) |
21 | 62 | } |
22 | 63 | } |
23 | 64 |
|
24 | 65 | public var _updateConfigurationHandler: |
25 | 66 | @MainActor (_ cell: VersatileCell, _ state: UICellConfigurationState) -> Void = { _, _ in } |
26 | 67 |
|
| 68 | + private var _highlightAnimation: any CellHighlightAnimation = .disabled |
| 69 | + |
27 | 70 | public override init( |
28 | 71 | frame: CGRect |
29 | 72 | ) { |
@@ -92,4 +135,9 @@ open class VersatileCell: UICollectionViewCell { |
92 | 135 | } |
93 | 136 | } |
94 | 137 |
|
| 138 | + public func highlightAnimation(_ animation: any CellHighlightAnimation) -> Self { |
| 139 | + self._highlightAnimation = animation |
| 140 | + return self |
| 141 | + } |
| 142 | + |
95 | 143 | } |
0 commit comments