0

Ich versuche, mein Menü zu animieren, das mit UICollectionView gemacht hat. Ich habe 2 weitere Ansichten, die Sie unten sehen können.UICollectionViewCell Animation mit Protokoll

Main Screen

Und ich versuche obere Leiste zu animieren, wenn nach unten gescrollt. Wenn die Animation abgeschlossen ist, wird die "Pokemon" -Etikette weiß, das Pokemon-Bild wird ausgeblendet und der Hintergrund wird blau sein. Ich benutze Protokoll, um Zelle zu erreichen. Dies ist meine ViewController-Klasse.

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

Und das ist cellForRowAt Funktion

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) 
    } 
} 

und zum Nachweis von Scrollen nach unten;

 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() 
       } 
     } 
    } 

Und das ist die TopCategoriesCell Klasse

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) 
} 

Eigentlich fein alles funktioniert. Aber nur das erste Element verschwindet, die anderen bleiben dort so;

MainVC After Animation

Wie kann ich andere Zelle Bilder verstecken?

Vielen Dank.

+0

Von wo werden Sie den Wert von 'self.categoryImage' Einstellung? –

+0

Ich habe es auf Storyboard gesetzt, ich bin jetzt im Designteil. – Azat

+0

Und sind diese Top-Bar Pokemon Bilder in CollectionView? –

Antwort

1

Sie haben den Zelldelegaten des Ansichtscontrollers als Zelle festgelegt. Dies scheint unnötig zu sein.

Es bedeutet auch, dass, da es nur einen View-Controller gibt und Delegaten ein Eins-zu-Eins-Kommunikationsmuster sind, Ihr View-Controller nur das Bild in einer Zelle versteckt.

diesen Einsatz zu beheben NotificationCenter im TopCategoriesCell initialiser wie folgt:

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) 
} 

Beachten Sie, dass die init Funktion Sie es in hängt davon ab, legen Sie, wie Sie die Zelle instanziieren.

Dann in Ihrem View-Controller:

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()    
    } 
} 

Und in der TopCategoriesCell:

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

Danke, es hat funktioniert. – Azat