我在使用RxSwift
a 的单元格动画时遇到问题UICollectionView
,我的简单设置如下:
collectionView.register(UINib(nibName: "CustomCollectionCell", bundle: nil), forCellWithReuseIdentifier: "cell")
let dataSource = RxCollectionViewSectionedAnimatedDataSource<SectionOfCustomDataAnimated>(
animationConfiguration: AnimationConfiguration(insertAnimation: .bottom, reloadAnimation: .bottom, deleteAnimation: .bottom),
configureCell: { dataSource, cv, indexPath, element in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionCell
cell.colorView.backgroundColor = element.color
return cell
})
像这样使用单元格和数据模型:
struct CustomDataAnimated {
let id: Int
let color: UIColor
}
extension CustomDataAnimated: IdentifiableType, Equatable {
typealias Identity = Int
var identity: Identity {
return id
}
}
struct SectionOfCustomDataAnimated {
var items: [Item]
// Need to provide a unique id, only one section in our model
var identity: Int {
return 0
}
}
extension SectionOfCustomDataAnimated: AnimatableSectionModelType {
typealias Identity = Int
typealias Item = CustomDataAnimated
init(original: SectionOfCustomDataAnimated, items: [Item]) {
self = original
self.items = items
}
}
我正在使用按下按钮BehaviourRelay
时更新的:update
private let sections = BehaviorRelay<[SectionOfCustomDataAnimated]>(
value: [SectionOfCustomDataAnimated(items: [
CustomDataAnimated(id: 0, color: .red),
CustomDataAnimated(id: 1, color: .yellow)
])])
@IBAction func didTapUpdate(_ sender: Any) {
let colors: [UIColor] = [.red, .blue, .green, .purple, .orange]
let originalColors = sections.value.first!.items
self.sections.accept([SectionOfCustomDataAnimated(items: originalColors + [CustomDataAnimated(id: originalColors.count ,color: colors.randomElement()!)])])
}
问题是集合视图确实有动画,但它似乎总是使用淡入淡出风格的动画。选择一个不同的选项,.bottom
例如上面的例子,仍然会产生相同的淡入淡出动画。我以前在表格视图上使用过类似的逻辑并且没有问题,我似乎只在集合视图中遇到问题。如何让不同风格的动画工作?