我在视图上有一个logoImage,为约束设置动画。 动画的约束是宽度和高度;从原始状态220.0到240.0(工作正常)然后我想让它回到220.0但动画被跳过,图像直接返回0.1秒到220.0。
这是动画的代码
func animate() {
self.imageHeightConstraint.constant = 240.0
self.imageWidthConstraint.constant = 240.0
UIView.animate(withDuration: 1.0,
delay:0.0,
options: .curveEaseIn,
animations:{
self.logoImage.layoutIfNeeded()
}, completion: { (finished: Bool) -> Void in
self.imageHeightConstraint.constant = 220.0
self.imageWidthConstraint.constant = 220.0
UIView.animate(withDuration: 2.0,
delay:0.0,
options: .curveEaseIn,
animations: {
self.logoImage.layoutIfNeeded()
})
})
}
提前感谢您提供的任何帮助! ; - )
答案 0 :(得分:1)
在重新设置常量之前,需要在完成块中调用self.logoImage.setNeedsLayout()
。这是因为您需要使接收器的当前布局无效并在下一个更新周期中触发布局更新。
请尝试使用此代码,它将适用于您:
func animate() {
self.imageHeightConstraint.constant = 240.0
self.imageWidthConstraint.constant = 240.0
UIView.animate(withDuration: 1.0,
delay:0.0,
options: .curveEaseIn,
animations:{
self.logoImage.layoutIfNeeded()
}, completion: { (finished: Bool) -> Void in
self.logoImage.setNeedsLayout()
self.imageHeightConstraint.constant = 220.0
self.imageWidthConstraint.constant = 220.0
UIView.animate(withDuration: 2.0,
delay:0.0,
options: .curveEaseIn,
animations: {
self.logoImage.layoutIfNeeded()
})
})
}