0

UICollectionView로 만든 메뉴에 애니메이션을 적용하려고합니다. 아래에서 볼 수있는 2 개의 콜렉션 뷰가 있습니다.UICollectionViewCell 애니메이션이있는 프로토콜

Main Screen

그리고 아래로 스크롤 할 때 상단 표시 줄에 애니메이션을 시도하고있다. 애니메이션이 완료되면 "포켓몬"레이블이 흰색으로 표시되고 포켓몬 이미지는 숨겨지고 배경은 파란색으로 표시됩니다. 나는 세포에 도달하기 위해 프로토콜을 사용합니다. 이것은 내 ViewController 클래스입니다.

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

그리고 이것은

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

상하 스크롤 감지 cellForRowAt

함수이고;

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

는 그리고이

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

은 사실 모든 작품을 잘 TopCategoriesCell 클래스입니다. 그러나 첫 번째 요소 만 사라지고 다른 요소는 이와 같이 유지됩니다.

MainVC After Animation

어떻게 다른 세포의 이미지를 숨길 수 있습니까?

감사합니다.

+0

위치를 'self.categoryImage'의 값을 설정하는에서? –

+0

스토리 보드에 설치했는데, 지금 디자인 부분에 붙어 있습니다. – Azat

+0

그리고 collectionView의 최상위 포켓몬 이미지입니까? –

답변

1

뷰 컨트롤러의 셀 대리자를 셀로 설정했습니다. 이것은 불필요한 것으로 보인다.

또한 하나의보기 컨트롤러 만 있고 대리자는 일대일 통신 패턴이므로보기 컨트롤러는 하나의 셀에만 이미지를 숨기는 것을 의미합니다. 다음과 같이

TopCategoriesCell initialiser이 사용 NotificationCenter를 해결하려면 :

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

참고 것을 당신이 배치 init 기능을 사용하면 셀을 인스턴스화하는 방법에 달려있다.

그런 다음 뷰 컨트롤러에서 :

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 :

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