gpt4 book ai didi

ios - RxDataSources Collection View 单元格始终使用淡入淡出来插入单元格动画,无法更改为不同的动画

转载 作者:行者123 更新时间:2023-11-28 23:20:18 27 4
gpt4 key购买 nike

我在 UICollectionView 上使用 RxSwift 时遇到单元格动画问题,我的简单设置如下:

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()!)])])
}

enter image description here

问题是 Collection View 动画但是它似乎总是使用淡入淡出风格的动画。在上面的示例中选择不同的选项(例如 .bottom)仍然会产生相同的淡入淡出动画。我以前在 TableView 上使用过类似的逻辑并且没有问题,我似乎只在 Collection View 中有问题。如何让不同风格的动画发挥作用?

最佳答案

UICollectionView 插入/删除动画由布局处理,因此 RxCollectionViewSectionedAnimatedDataSource 无法为您完成。例如,如果您查看 insertRows/insertItems 函数,您会注意到 UITableView UICollectionView 需要动画才不是。您看到的是 UICollectionViewFlowLayout 使用的默认动画,它是淡入淡出的动画。如果你想要一个自定义行为,你必须创建一个布局子类,并且有很多 tutorials为此。

关于ios - RxDataSources Collection View 单元格始终使用淡入淡出来插入单元格动画,无法更改为不同的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59727464/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com