如何使用自动布局锚点为 UICollectionView 设置动画?
How do you animate a UICollectionView using auto layout anchors?
我正在尝试使用布局约束为集合视图设置动画,但我无法让它工作。我有一个占据整个屏幕的 collectionview,在点击按钮时我想将 collectionview 向上移动以为从底部进入的另一个视图腾出空间 - 见下图
传入的 UIView 动画效果很好(视图从底部升起)- 我想移动集合视图的原因是传入的 UIView 遮挡了集合视图,所以我只是试图同时向上移动集合视图时间作为新视图,以便可以显示 collectionview 中的所有内容而不会被新视图隐藏 - 我使用参考视图为 collectionview 的最终位置获取正确的布局约束 Image to show what I am trying to achieve 我是吗以正确的方式去做?
下面的代码示例没有发生任何事情,我不确定从这里到哪里去 - 同样的方法用于动画传入视图并且工作得很好但似乎不适用于集合视图......
如有任何帮助,我们将不胜感激
var colViewBottomToReferenceTop: NSLayoutConstraint?
var colViewBottomToViewBottom: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
colViewBottomToReferenceTop = musicCollectionView.bottomAnchor.constraint(equalTo: referenceView.topAnchor)
colViewBottomToViewBottom = musicCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
NSLayoutConstraint.active([colViewBottomToViewBottom!])
NSLayoutConstraint.deactivate([colViewBottomToReferenceTop!])
}
func playerShow() {
NSLayoutConstraint.activate([colViewBottomToReferenceTop!])
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
@IBAction func btnTapped(_ sender: UIButton) {
playerShow()
}
您想在动画之前停用另一个
NSLayoutConstraint.deactivate([colViewBottomToViewBottom!])
NSLayoutConstraint.activate([colViewBottomToReferenceTop!])
看这个Demo
我正在尝试使用布局约束为集合视图设置动画,但我无法让它工作。我有一个占据整个屏幕的 collectionview,在点击按钮时我想将 collectionview 向上移动以为从底部进入的另一个视图腾出空间 - 见下图
传入的 UIView 动画效果很好(视图从底部升起)- 我想移动集合视图的原因是传入的 UIView 遮挡了集合视图,所以我只是试图同时向上移动集合视图时间作为新视图,以便可以显示 collectionview 中的所有内容而不会被新视图隐藏 - 我使用参考视图为 collectionview 的最终位置获取正确的布局约束 Image to show what I am trying to achieve 我是吗以正确的方式去做?
下面的代码示例没有发生任何事情,我不确定从这里到哪里去 - 同样的方法用于动画传入视图并且工作得很好但似乎不适用于集合视图......
如有任何帮助,我们将不胜感激
var colViewBottomToReferenceTop: NSLayoutConstraint?
var colViewBottomToViewBottom: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
colViewBottomToReferenceTop = musicCollectionView.bottomAnchor.constraint(equalTo: referenceView.topAnchor)
colViewBottomToViewBottom = musicCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
NSLayoutConstraint.active([colViewBottomToViewBottom!])
NSLayoutConstraint.deactivate([colViewBottomToReferenceTop!])
}
func playerShow() {
NSLayoutConstraint.activate([colViewBottomToReferenceTop!])
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
@IBAction func btnTapped(_ sender: UIButton) {
playerShow()
}
您想在动画之前停用另一个
NSLayoutConstraint.deactivate([colViewBottomToViewBottom!])
NSLayoutConstraint.activate([colViewBottomToReferenceTop!])
看这个Demo