RxDataSources 集合视图单元格始终使用淡入淡出插入单元格动画,无法更改为不同的动画 [英] RxDataSources collection view cell always uses fade for insert cell animation, can't change to a different animation
问题描述
我在 UICollectionView
上使用 RxSwift
的单元格动画有问题,我的简单设置如下:
I am having an issue with cell animations using RxSwift
on a UICollectionView
, my simple setup is as follows:
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
})
像这样的单元格和数据模型:
With the cell and data models like so:
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
按钮时更新:
I am using a BehaviourRelay
that updates when the update
button is pressed:
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
仍然会产生相同的淡入淡出动画.我之前在表视图上使用过类似的逻辑,没有问题,我似乎只有集合视图有问题.如何让不同风格的动画发挥作用?
The problem is that the collection view does animate however it seems it always uses a fade style animation. Choosing a different option such as .bottom
in the example above still results in the same fade animation. I have used similar logic on a table view before and there was no issue, I only seem to have the issue in collection views. How can I get the different style of animations to work?
推荐答案
UICollectionView
插入/删除动画由布局处理,因此 RxCollectionViewSectionedAnimatedDataSource
无法为您做到这一点.例如,如果您查看 insertRows
/insertItems
funcs,您会注意到对于 UITableView
它需要动画而 UICollectionView
没有.您看到的是 UICollectionViewFlowLayout
使用的默认动画,它是淡入淡出动画.如果您想要自定义行为,您必须创建一个布局子类,并且有很多 教程.
UICollectionView
insertion/deletion animations are handled by the layout so the RxCollectionViewSectionedAnimatedDataSource
cannot do that for you. If you take a look at the insertRows
/insertItems
funcs for example, you will notice that for UITableView
it takes animation while for UICollectionView
does not. What you are seeing is the default animation used by UICollectionViewFlowLayout
which is a fade animation. If you want a custom behavior you have to create a layout subclass and there are plenty of tutorials for that.
这篇关于RxDataSources 集合视图单元格始终使用淡入淡出插入单元格动画,无法更改为不同的动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!