UICollectionView高度的动画看起来很奇怪,单元格会缩小

时间:2018-03-13 19:45:34

标签: ios swift uicollectionview

我正在尝试在UITableViewCell中显示UICollectionView。最初不应显示CollectionView,但是当用户按下按钮时,CollectionView将变为可见的动画。我得到了这个工作,但是当CollectionView第一次变得可见时,看起来细胞被缩小了,如果我隐藏了CollectionView并再次展开它,动画看起来是正确的:

http://g.recordit.co/DBhZCmJKPj.gif

这是动画的代码:

func expand() {
    tableView?.beginUpdates()
    UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveLinear, animations: {
        self.imageViewDisclosureIndicator.setImage(UIImage(named: "arrow-up"), for: .normal)
        self.collectionViewHeight.constant = self.collectionView.intrinsicContentSize.height
        self.layoutIfNeeded()
        self.isExpanded = true
    }, completion: nil)
    tableView?.endUpdates()
}

func collapse() {
    tableView?.beginUpdates()
    UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveLinear, animations: {
        self.imageViewDisclosureIndicator.setImage(UIImage(named: "arrow-down"), for: .normal)
        self.collectionViewHeight.constant = CGFloat(0.0)
        self.layoutIfNeeded()
        self.isExpanded = false
    }, completion: nil)
    tableView?.endUpdates()
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

尝试将self.layoutIfNeeded置于动画之外。

根据您的代码,我不明白为什么您需要这一行代码,但这可能是您的问题的原因。我认为内容甚至可以在没有该行的情况下正常加载,因为集合视图的内容视图的大小与集合视图的高度无关。

相关问题