带有协议的 UICollectionViewCell 动画
UICollectionViewCell Animation With Protocol
我正在尝试为使用 UICollectionView 制作的菜单制作动画。我还有 2 个集合视图,您可以在下面看到它们。
我正在尝试在向下滚动时为顶部栏设置动画。动画完成后,"Pokemon" 标签将变为白色,口袋妖怪图像将被隐藏,背景将变为蓝色。我使用协议到达单元格。这是我的 ViewController class.
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 class
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
初始化程序中使用 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)
}
我正在尝试为使用 UICollectionView 制作的菜单制作动画。我还有 2 个集合视图,您可以在下面看到它们。
我正在尝试在向下滚动时为顶部栏设置动画。动画完成后,"Pokemon" 标签将变为白色,口袋妖怪图像将被隐藏,背景将变为蓝色。我使用协议到达单元格。这是我的 ViewController class.
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 class
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
初始化程序中使用 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)
}