如何解决使用rx在collectionView中单击时如何获取我的单元格?
在我的旧项目中,我点击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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。