相关疑难解决方法(0)

`setCollectionViewLayout` 动画在更改集合视图框架时被破坏

我正在尝试在水平胶片布局和垂直堆栈布局之间转换我的收藏视图。

水平膜条 垂直/扩展堆栈
水平对齐的单元格 单元格垂直堆叠

这就是我目前正在做的事情:

  1. 使用setCollectionViewLayout(_:animated:completion:)水平和垂直滚动方向之间的过渡
  2. 更改集合视图的高度约束,随着UIView.animateself.view.layoutIfNeeded()

从胶片到垂直堆栈的动画很好。但是,从垂直堆栈到胶片的动画被破坏了。这是它的样子:

在胶片条和扩展堆栈之间转换,但返回时出现故障

如果我collectionViewHeightConstraint默认设置为 300,并删除这些行:

collectionViewHeightConstraint.constant = 300
collectionViewHeightConstraint.constant = 50
Run Code Online (Sandbox Code Playgroud)

...过渡动画两种方式都很好。但是,有多余的间距,我希望胶片布局仅在 1 行中。

在胶片和扩展堆栈之间转换,没有故障,但胶片布局有 5 行和 2 列而不是 1 行

如何使动画双向流畅?这是我的代码(链接到演示项目):

class ViewController: UIViewController {

    var isExpanded = false
    var verticalFlowLayout = UICollectionViewFlowLayout()
    var horizontalFlowLayout = UICollectionViewFlowLayout()
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
    @IBAction func toggleExpandPressed(_ sender: Any) {
        
        isExpanded.toggle()
        if isExpanded {
            collectionView.setCollectionViewLayout(verticalFlowLayout, animated: true) /// set vertical scroll
            collectionViewHeightConstraint.constant = 300 /// make …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview swift

5
推荐指数
1
解决办法
219
查看次数

标签 统计

ios ×1

swift ×1

uicollectionview ×1