如何在 RxSwift 中对整个应用程序中的单元格和按钮进行动画处理



我想在我的应用程序中的每个按钮和单元格上制作点击动画,基本上所有带有操作的视图。每次点击我都想要一个灰色的眨眼。

问题如下:我为按钮创建了一个自定义类,整个应用程序中的按钮工作正常。问题出在单元格上,因为我试图使动画像 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,以便它们在应用程序中可见?也许我想多了,有更简单的方法?提前感谢

我可以推荐RxDataSources

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

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

最新更新