簡體   English   中英

使用 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時如何獲得cell 我設法像這樣掌握了modelcell

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM