In my old project I did a small animation when clicking a 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)
}
}
Now I'm trying to refactor my project using rx swift
, but I'm not sure how to get ahold of the cell
when clicking one in the collectionView
. I have managed to get a hold of the model
and the cell
like this:
Observable.zip(myCollectionView.rx.itemSelected, myCollectionView.rx.modelSelected(RecipesCollectionViewCellViewModel.self))
.bind { indexPath, model in
print(model)
}.disposed(by: disposeBag)
But how do I get ahold of the cell
so that I can do my animation stuff?
Using whichever variable you assigned to your collection view, I normally access it using something like this in my 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)
Based on your example, you can use the same idea and access your cell using the cellForItem(at:)
function:
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)