0

UICollectionViewで作成したメニューをアニメーション表示しようとしています。私はあなたが下記で見ることができる2つのコレクションビューをもう1つ持っています。UICollectionViewCellアニメーションプロトコル付き

Main Screen

そして、私はスクロールダウン時に上部のバーをアニメーション化しようとしています。アニメーションが完成すると、「ポケモン」のラベルは白くなり、ポケモンのイメージは隠され、背景は青色になります。私はプロトコルを使って細胞に到達します。これは私のViewControllerクラスです。

protocol TopCategoriesDelegator{ 
func openTopBar() 
func closeTopBar()} 

そして、これはcellForRowAt機能

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if collectionView == self.collectionView{ 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topCategory", for: indexPath) as! TopCategoriesCell 

     cell.viewController = self 
     return cell 
    }else if collectionView == subCategoriesCollectionView{ 
     return collectionView.dequeueReusableCell(withReuseIdentifier: "subCategories", for: indexPath) 
    }else{ 
     return collectionView.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath) 
    } 
} 

そして、スクロールダウンの検出のためにあります。

 func scrollViewDidScroll(_ scrollView: UIScrollView) { 
//   print(scrollView.contentOffset.y) 
     if scrollView.contentOffset.y > 160 { 
       if self.cellDelegate != nil{ 
        self.cellDelegate.closeTopBar() // func in main vc for background color and size 
        closeAnimation() 
       } 
     }else{ 
       if self.cellDelegate != nil{ 
        self.cellDelegate.openTopBar() // func in main vc for background color and size 
        openAnimation() 
       } 
     } 
    } 

そして、これがTopCategoriesCellクラス

override func layoutSubviews() { 

    if let vc = viewController as? ProductsVC{ 
     vc.cellDelegate = self 
    } 
} 
func closeTopBar() { 
    UIView.animate(withDuration: 0.3) { 
     self.categoryImage.frame = CGRect(x: 0, y: 0, width: 0, height: 0) 
     self.categoryImage.isHidden = true 
    } 
} 
func openTopBar(){ 
    UIView.transition(with: categoryImage, duration: 0.3, options: .curveEaseIn, animations: { 
     self.categoryImage.isHidden = false 
     self.categoryName.textColor = UIColor().rgb(red: 37.0, green: 110.0, blue: 140.0) 
    }, completion: nil) 
} 

は実際にすべてが正常に動作しています。しかし、最初の要素だけが消えて、他の要素はこのようにそこにとどまります。

MainVC After Animation

どのように私は他の細胞の画像を非表示にできますか?

ありがとうございます。

+0

func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.contentOffset.y > 160 { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "scrolledDown"), object: nil) } else { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "scrolledUp"), object: nil) openAnimation() } } 

そしてTopCategoriesCellで? –

+0

私はストーリーボードにセットしました。 – Azat

+0

そしてcollectionViewのトップバーのポケモンの画像は? –

答えて

1

ビューコントローラのセルデリゲートをセルに設定しました。これは不要です。

これは、1つのビューコントローラしかなく、デリゲートが1対1の通信パターンであるため、ビューコントローラは1つのセルにのみイメージを隠すことを意味します。

次のようにTopCategoriesCell初期化子でこの使用NotificationCenterを修正するには:init機能あなたはセルをインスタンス化しているかに依存して、それを置くこと

init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    NotificationCenter.default.addObserver(self, selector: #selector(TopCategoriesCell.closeTopBar), name: NSNotification.Name(rawValue: "scrolledDown"), object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(TopCategoriesCell.openTopBar), name: NSNotification.Name(rawValue: "scrolledUp"), object: nil) 
} 

注意を。

次に、あなたのビューコントローラで:あなたは 'self.categoryImage' の値を設定しているところから、

deinit { 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "scrolledDown"), object: nil) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "scrolledUp"), object: nil) 
} 
+0

ありがとう、それは働いた。 – Azat

関連する問題