I wonder how to make an animation on a view inside a cell visible on-screen based on iOS 14 cell configuration API.
Imagine I want to animate the visibility of a placeholder view, in the old style, I would have write something like this:
WWDC 2020 "Modern Cell Configuration" session briefly mentions animations at 09:09 time. It says that we can nest a configuration change inside an animation block.
In practice, I'm not sure how I could do this because every configuration change needs to call setNeedsUpdateConfiguration(). Since it's an async method, nesting this method in an animation block would not work.
Here is a code snippet of a custom cell based on a cell configuration API:
Apple recommendation is to always write all the cell’s configuration in a single place updateConfiguration(using state: UICellConfigurationState). Since this method is called by setNeedsUpdateConfiguration()which is async, I don't know how I could apply an animation when changing the alpha value of the placeholderView.
Imagine I want to animate the visibility of a placeholder view, in the old style, I would have write something like this:
Code Block class MyCell: UICollectionViewCell { var placeholderVisible: Bool { get { return placeholderVisibleBackingValue } set { setPlaceholderVisible(newValue, animated: false) } } func setPlaceholderVisible(_ visible: Bool, animated: Bool) { placeholderVisibleBackingValue = visible updatePlaceHolderVisibility(animated: animated) } private func updatePlaceHolderVisibility(animated: Bool) { if animated { UIView.animate(withDuration: 0.5) { placeholderView.alpha = placeholderVisible ? 1 : 0 } } else { placeholderView.alpha = placeholderVisible ? 1 : 0 } } }
WWDC 2020 "Modern Cell Configuration" session briefly mentions animations at 09:09 time. It says that we can nest a configuration change inside an animation block.
In practice, I'm not sure how I could do this because every configuration change needs to call setNeedsUpdateConfiguration(). Since it's an async method, nesting this method in an animation block would not work.
Here is a code snippet of a custom cell based on a cell configuration API:
Code Block class MyCell: UICollectionViewListCell { var placeholderVisible: Bool = false { didSet { guard oldValue != placeholderVisible else { return } setNeedsUpdateConfiguration() } } override var configurationState: UICellConfigurationState { var state = super.configurationState /* `state.placeholderVisible` is a custom key in the UICellConfigurationState key value store */ state.placeholderVisible = placeholderVisible return state } override func updateConfiguration(using state: UICellConfigurationState) { var content = defaultListContentConfiguration().updated(for: state) listContentView.configuration = content placeholderView.alpha = state.placeholderVisible ? 1 : 0 } }
Apple recommendation is to always write all the cell’s configuration in a single place updateConfiguration(using state: UICellConfigurationState). Since this method is called by setNeedsUpdateConfiguration()which is async, I don't know how I could apply an animation when changing the alpha value of the placeholderView.