如何在RxSwift中为整个应用程序中的单元格和按钮设置动画?

时间:2019-06-03 11:45:47

标签: swift animation uicollectionviewcell rx-swift

我想在应用程序中的每个按钮和单元上制作单击动画,基本上是所有带有动作的视图。每次单击时,我都希望闪烁灰色。

问题如下:我为按钮创建了一个自定义类,并且应用程序中的按钮运行正常。问题出在我尝试使动画类似于rx的单元格上。我有UITableView单元格以及UICollectionView单元格以及一些在这里和那里的视图,所以我尝试做UIView扩展,它可以识别水龙头,如果有的话-可以播放动画。我尝试通过扩展名做到这一点:

import UIKit
import RxSwift

extension UIView {
    func makeSelectionIndicatable(forInitialBackgroundColor color: UIColor) {
        let tapGesture = UILongPressGestureRecognizer()
        tapGesture.minimumPressDuration = 0
        tapGesture.cancelsTouchesInView = false
        addGestureRecognizer(tapGesture)

        tapGesture.rx.event.bind(onNext: { [weak self] recognizer in
            if recognizer.state == .began {
                self?.backgroundColor = .global(color: .athens_gray)
            }
            if recognizer.state == .ended {
                self?.backgroundColor = color
            }
            if recognizer.state == .cancelled {
                self?.backgroundColor = color
            }
        })
    }
}

显然没有处置,但是如果我在扩展名中添加一个,则没有任何效果。我的问题是:如何制作类似于rx的动画,以便它们在整个应用程序中可见?也许我想得太多,有更简单的方法吗?提前谢谢

1 个答案:

答案 0 :(得分:1)

我可以推荐RxDataSources

data.bind(to: tableView.rx.items(cellIdentifier: "Cell")) { index, model, cell in
  cell.textLabel?.text = model
}
  

RxDataSources提供了两种特殊的数据源类型,它们可以自动处理绑定数据源中的动画变化:RxTableViewSectionedAnimatedDataSource和RxCollectionViewSectionedAnimatedDataSource