问题描述
在我的旧项目中,我点击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
重构项目,但是我不确定在cell
中单击一个时如何获取collectionView
。我设法像这样抓住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)