我试图在 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()
}
如有任何帮助,我们将不胜感激!
最佳答案
尝试将 self.layoutIfNeeded
放在动画之外。
根据您的代码,我不明白您为什么需要该行代码,但这可能是导致您的问题的原因。我认为没有那条线内容甚至可以正常加载,因为 Collection View 的内容 View 的大小与 Collection View 的高度无关。
关于ios - UICollectionView 高度的动画看起来很奇怪,单元格缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49264680/