1

我在隐藏UICollectionView. 显示动画效果很好,但是当我执行隐藏动画时,它会立即隐藏没有动画的集合视图。这是代码:

@objc func openMenu(sender: UIButton) {
        if sender.tag == 1 {
            self.buttonView.tag = 2
            self.arrow.image = UIImage(named: "arrowUp.png")
            UIView.animate(withDuration: 0.7, animations: {
                self.moduleView.frame.size.height = UIScreen.main.bounds.size.height - self.frame.size.height
            }, completion: { _ in
            })
        } else {
            self.buttonView.tag = 1
            self.arrow.image = UIImage(named: "arrowDown.png")
            UIView.animate(withDuration: 0.7, animations: {
                self.moduleView.frame.size.height = 0
            }, completion: { _ in
            })
        }
    }  

输出 :

在此处输入图像描述

奇怪的是,我用一个简单的替换了集合视图,UIView它工作正常。自下而上的动画效果完美。代码 :

@objc func openMenu(sender: UIButton) {
        if sender.tag == 1 {
            self.buttonView.tag = 2
            self.arrow.image = UIImage(named: "arrowUp.png")
            UIView.animate(withDuration: 0.7, animations: {
                self.testView.frame.size.height = UIScreen.main.bounds.size.height - self.frame.size.height
            }, completion: { _ in
            })
        } else {
            self.buttonView.tag = 1
            self.arrow.image = UIImage(named: "arrowDown.png")
            UIView.animate(withDuration: 0.7, animations: {
                self.testView.frame.size.height = 0
            }, completion: { _ in
            })
        }
    }  

输出 :

在此处输入图像描述

问题:为什么这对UICollectionView 不起作用

初始化:

UICollectionView:

self.moduleView = ModulesCollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0), collectionViewLayout: UICollectionViewLayout())  
self.parentView.addSubView(self.moduleView)  

界面视图:

self.testView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0))  
self.parentView.addSubView(self.testView)
4

1 回答 1

1

您需要使用layoutSubViews()适当的动画方法。请更改您的代码如下:

@objc func openMenu(sender: UIButton) {
    if sender.tag == 1 {
        self.buttonView.tag = 2
        self.arrow.image = UIImage(named: "arrowUp.png")
        UIView.animate(withDuration: 0.7, animations: {
            self.moduleView.frame.size.height = UIScreen.main.bounds.size.height - self.frame.size.height
            // Add this line
            self.moduleView.layoutSubviews()
        }, completion: { _ in
        })
    } else {
        self.buttonView.tag = 1
        self.arrow.image = UIImage(named: "arrowDown.png")
        UIView.animate(withDuration: 0.7, animations: {
            self.moduleView.frame.size.height = 0
            // Add this line
            self.moduleView.layoutSubviews()
        }, completion: { _ in
        })
    }
}  
于 2018-06-14T06:59:36.823 回答