【发布时间】:2017-09-15 11:53:52
【问题描述】:
我正在尝试为使用 UICollectionView 制作的菜单制作动画。我还有 2 个集合视图,您可以在下面看到。
我正在尝试在向下滚动时为顶部栏设置动画。动画完成后,“口袋妖怪”标签为白色,口袋妖怪图像被隐藏,背景为蓝色。我使用协议到达单元格。这是我的 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)
}
实际上一切正常。但是只有第一个元素消失了,其他元素就这样呆在那里;
如何隐藏其他单元格的图像?
谢谢。
【问题讨论】:
-
你从哪里设置'self.categoryImage'的值?
-
我把它设置在故事板上,我现在在设计部分。
-
那些顶栏口袋妖怪图像在 collectionView 中吗?
-
是的,它在collectionView中,并且单元格是TopCategoriesCell。
标签: ios swift animation uicollectionview uicollectionviewcell