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

标签 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()
}

如有任何帮助,我们将不胜感激!

最佳答案

尝试将 self.layoutIfNeeded 放在动画之外。

根据您的代码,我不明白您为什么需要该行代码,但这可能是导致您的问题的原因。我认为没有那条线内容甚至可以正常加载,因为 Collection View 的内容 View 的大小与 Collection View 的高度无关。

关于ios - UICollectionView 高度的动画看起来很奇怪,单元格缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49264680/

相关文章:

javascript - 什么是 "native",什么不在 React Native 项目中?

ios - 设置RubyMotion应用以播放背景音频

ios - 如何在 swift 2 中解析未知的 json 数据类型

swift - 符合通用协议(protocol)的多个实例

ios - 快速使用collectionview滑动图像

Swift - 我的收藏 View 没有输出额外的数量

ios - 具有不同大小单元格的 UICollectionView

ios - 如何使用swift在IOS App中缓存过期图像

swift - Vapor 不通过 Leaf 渲染文件

ios - UICollectionView 滚动到没有动画的最后一项