如何为依赖约束设置动画
how to animate dependent constraints
我正在尝试同时对同一视图的 2 个约束(确定 y 轴上的位置和高度)设置动画。问题是,它们是依赖的,我的意思是,它们一起工作来确定相同的属性。图说的就是我的意思
2 个箭头表示约束。 upper 设置 upper view 和 bottom view 之间的距离,bottom constraints 设置 bottom layout guide 和 bottom view 之间的距离。底部视图从视图下方滑动并停在您当前看到的位置。高度是用动画约束设置的。这是我用来制作动画的代码:
self.toBottomConstraint.constant = 53
self.toUpperConstraint.constant = 15
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 20, initialSpringVelocity: 20, options: UIViewAnimationOptions.CurveLinear, animations: {
self.view.layoutIfNeeded()
}, completion: {_ in})
代码确实有效,但我收到此 错误。
无法同时满足约束条件。
可能至少以下列表中的约束之一是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints
,请参阅 UIView
属性 translatesAutoresizingMaskIntoConstraints
的文档)...
....
....
....
那么我应该如何为 2 个相关约束设置动画?
编辑://上视图约束。
//底部视图约束。 BuyButton 和 collection 视图嵌套在底部视图中
您就快完成了,只需将常量的更改移动到闭包中并提前调用 layoutIfNeeded()
:
self.layoutIfNeeded()
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 20, initialSpringVelocity: 20, options: UIViewAnimationOptions.CurveLinear, animations: {
self.toBottomConstraint.constant = 53
self.toUpperConstraint.constant = 15
self.view.layoutIfNeeded()
}, completion: {_ in})
您可以在 another answer about constraint changes.
中阅读有关动画约束的更多信息
根据您制作动画的方式,实际上有无数种方法可以在动画前后设置约束。最重要的是,您需要决定约束是什么,在动画之前,以及它们将在动画之后。例如:
self.toBottomConstraint.constant = 153
self.toUpperConstraint.constant = 0
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.toBottomConstraint.constant = 53
self.toUpperConstraint.constant = 15
self.view.layoutIfNeeded()
}, completion: nil)
上面的代码将使底部视图自下而上动画化。
顺便说一句,冲突约束的控制台警告消息可能是由您在底部视图上设置的除顶部和底部约束之外的其他约束引起的,例如,您可能为底部视图配置了高度约束,在这种情况下,您需要在动画结束后更新高度约束。
我正在尝试同时对同一视图的 2 个约束(确定 y 轴上的位置和高度)设置动画。问题是,它们是依赖的,我的意思是,它们一起工作来确定相同的属性。图说的就是我的意思
2 个箭头表示约束。 upper 设置 upper view 和 bottom view 之间的距离,bottom constraints 设置 bottom layout guide 和 bottom view 之间的距离。底部视图从视图下方滑动并停在您当前看到的位置。高度是用动画约束设置的。这是我用来制作动画的代码:
self.toBottomConstraint.constant = 53
self.toUpperConstraint.constant = 15
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 20, initialSpringVelocity: 20, options: UIViewAnimationOptions.CurveLinear, animations: {
self.view.layoutIfNeeded()
}, completion: {_ in})
代码确实有效,但我收到此 错误。
无法同时满足约束条件。
可能至少以下列表中的约束之一是您不想要的。试试这个:(1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints
,请参阅 UIView
属性 translatesAutoresizingMaskIntoConstraints
的文档)...
....
....
....
那么我应该如何为 2 个相关约束设置动画?
编辑://上视图约束。
//底部视图约束。 BuyButton 和 collection 视图嵌套在底部视图中
您就快完成了,只需将常量的更改移动到闭包中并提前调用 layoutIfNeeded()
:
self.layoutIfNeeded()
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 20, initialSpringVelocity: 20, options: UIViewAnimationOptions.CurveLinear, animations: {
self.toBottomConstraint.constant = 53
self.toUpperConstraint.constant = 15
self.view.layoutIfNeeded()
}, completion: {_ in})
您可以在 another answer about constraint changes.
中阅读有关动画约束的更多信息根据您制作动画的方式,实际上有无数种方法可以在动画前后设置约束。最重要的是,您需要决定约束是什么,在动画之前,以及它们将在动画之后。例如:
self.toBottomConstraint.constant = 153
self.toUpperConstraint.constant = 0
UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.toBottomConstraint.constant = 53
self.toUpperConstraint.constant = 15
self.view.layoutIfNeeded()
}, completion: nil)
上面的代码将使底部视图自下而上动画化。
顺便说一句,冲突约束的控制台警告消息可能是由您在底部视图上设置的除顶部和底部约束之外的其他约束引起的,例如,您可能为底部视图配置了高度约束,在这种情况下,您需要在动画结束后更新高度约束。