簡體   English   中英

RxDataSources 集合視圖單元格始終使用淡入淡出插入單元格動畫,無法更改為不同的動畫

[英]RxDataSources collection view cell always uses fade for insert cell animation, can't change to a different animation

我有使用電池動畫的問題RxSwiftUICollectionView ,我簡單的設置如下:

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按鈕時updateBehaviourRelay

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM