0

我在使用RxSwifta 的单元格动画时遇到问题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例如上面的例子,仍然会产生相同的淡入淡出动画。我以前在表格视图上使用过类似的逻辑并且没有问题,我似乎只在集合视图中遇到问题。如何让不同风格的动画工作?

4

1 回答 1

0

UICollectionView插入/删除动画由布局处理,因此RxCollectionViewSectionedAnimatedDataSource无法为您执行此操作。例如,如果您看一下insertRows/ insertItemsfuncs,您会注意到 forUITableView需要动画,而 forUICollectionView不需要。您所看到的是使用的默认动画UICollectionViewFlowLayout是淡入淡出动画。如果你想要一个自定义行为,你必须创建一个布局子类,并且有很多教程

于 2020-01-14T12:09:08.673 回答