[英]RxDataSources collection view cell always uses fade for insert cell animation, can't change to a different animation
我有使用電池動畫的問題RxSwift
上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
}
}
我正在使用按下update
按鈕時update
的BehaviourRelay
:
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
仍然會產生相同的淡入淡出動畫。 我之前在表視圖上使用過類似的邏輯並且沒有問題,我似乎只在集合視圖中有問題。 我怎樣才能讓不同風格的動畫工作?
UICollectionView
插入/刪除動畫由布局處理,因此RxCollectionViewSectionedAnimatedDataSource
無法為您執行此操作。 例如,如果您查看insertRows
/ insertItems
函數,您會注意到UITableView
需要動畫,而UICollectionView
則不需要。 您所看到的是UICollectionViewFlowLayout
使用的默認動畫,它是淡入淡出動畫。 如果你想要一個自定義行為,你必須創建一個布局子類,並且有很多教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.