我正在尝试在水平胶片布局和垂直堆栈布局之间转换我的收藏视图。
这就是我目前正在做的事情:
setCollectionViewLayout(_:animated:completion:)水平和垂直滚动方向之间的过渡UIView.animate和self.view.layoutIfNeeded()从胶片到垂直堆栈的动画很好。但是,从垂直堆栈到胶片的动画被破坏了。这是它的样子:
如果我collectionViewHeightConstraint默认设置为 300,并删除这些行:
collectionViewHeightConstraint.constant = 300
collectionViewHeightConstraint.constant = 50
Run Code Online (Sandbox Code Playgroud)
...过渡动画两种方式都很好。但是,有多余的间距,我希望胶片布局仅在 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)