【问题标题】:How do I get ahold of my cell when clicking in collectionView using rx?使用 rx 在 collectionView 中单击时如何获取我的单元格?
【发布时间】:2020-11-01 07:08:07
【问题描述】:

在我的旧项目中,我在单击 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。我设法得到了modelcell,如下所示:

Observable.zip(myCollectionView.rx.itemSelected, myCollectionView.rx.modelSelected(RecipesCollectionViewCellViewModel.self))
    .bind { indexPath, model in
        print(model)
}.disposed(by: disposeBag)

但是我如何获得cell 以便我可以制作动画?

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell rx-swift


    【解决方案1】:

    使用您分配给集合视图的任何变量,我通常在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)
    

    【讨论】:

    • 啊,那真是太好了。非常感谢您清除它! @Jeriel Ng
    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多