使用 rx 单击 collectionView 时如何获取我的单元格?
How do I get ahold of my cell when clicking in collectionView using rx?
在我的旧项目中,我在单击 cell
:
时做了一个小动画
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.transform = CGAffineTransform(scaleX: 1.15, y: 1.15)
UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .allowUserInteraction, animations: {
cell?.transform = CGAffineTransform.identity
}, completion: nil)
if self.fbHasLoaded == true {
performSegue(withIdentifier: "collectionViewToCookieRecipe", sender: indexPath.row)
}
}
现在我正在尝试使用 rx swift
重构我的项目,但我不确定在 collectionView
中单击一个时如何获得 cell
。我已经设法得到 model
和 cell
这样的:
Observable.zip(myCollectionView.rx.itemSelected, myCollectionView.rx.modelSelected(RecipesCollectionViewCellViewModel.self))
.bind { indexPath, model in
print(model)
}.disposed(by: disposeBag)
但是我怎样才能得到 cell
以便我可以做我的动画呢?
使用您分配给集合视图的任何变量,我通常在 viewDidLoad
:
中使用类似这样的方式访问它
myCollectionView.rx.itemSelected
.subscribe(onNext: { [weak self] indexPath in
let cell = self?.myCollectionView.cellForItem(at: indexPath)
// Perform your animation operations here
}).diposed(by: disposeBag)
根据您的示例,您可以使用相同的想法并使用 cellForItem(at:)
函数访问您的单元格:
Observable.zip(myCollectionView.rx.itemSelected, myCollectionView.rx.modelSelected(RecipesCollectionViewCellViewModel.self))
.bind { indexPath, model in
let cell = self?.myCollectionView.cellForItem(at: indexPath) as? MyCollectionViewCell
}.disposed(by: disposeBag)
在我的旧项目中,我在单击 cell
:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.transform = CGAffineTransform(scaleX: 1.15, y: 1.15)
UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.5, options: .allowUserInteraction, animations: {
cell?.transform = CGAffineTransform.identity
}, completion: nil)
if self.fbHasLoaded == true {
performSegue(withIdentifier: "collectionViewToCookieRecipe", sender: indexPath.row)
}
}
现在我正在尝试使用 rx swift
重构我的项目,但我不确定在 collectionView
中单击一个时如何获得 cell
。我已经设法得到 model
和 cell
这样的:
Observable.zip(myCollectionView.rx.itemSelected, myCollectionView.rx.modelSelected(RecipesCollectionViewCellViewModel.self))
.bind { indexPath, model in
print(model)
}.disposed(by: disposeBag)
但是我怎样才能得到 cell
以便我可以做我的动画呢?
使用您分配给集合视图的任何变量,我通常在 viewDidLoad
:
myCollectionView.rx.itemSelected
.subscribe(onNext: { [weak self] indexPath in
let cell = self?.myCollectionView.cellForItem(at: indexPath)
// Perform your animation operations here
}).diposed(by: disposeBag)
根据您的示例,您可以使用相同的想法并使用 cellForItem(at:)
函数访问您的单元格:
Observable.zip(myCollectionView.rx.itemSelected, myCollectionView.rx.modelSelected(RecipesCollectionViewCellViewModel.self))
.bind { indexPath, model in
let cell = self?.myCollectionView.cellForItem(at: indexPath) as? MyCollectionViewCell
}.disposed(by: disposeBag)