我正在尝试为UIImageView
制作动画,基本上在整个视图在屏幕上的原始位置。只要我按下一个按钮,视图就会向左滑动,只能看到它的一部分。 constraint.constant的值为:
这是代码。
func constraintAnimation (duration: Double, animatedConstraint: NSLayoutConstraint, finalConstantValue: CGFloat) {
self.view.layoutIfNeeded()
UIImageView.animateWithDuration(duration, animations: {
animatedConstraint.constant = finalConstantValue
})
self.view.layoutIfNeeded()
println("")
}
答案 0 :(得分:4)
将您的代码更改为此并尝试
func constraintAnimation (duration: Double, animatedConstraint: NSLayoutConstraint, finalConstantValue: CGFloat) {
animatedConstraint.constant = finalConstantValue
UIImageView.animateWithDuration(duration, animations: {
self.view.layoutIfNeeded()
})
println("")
}
答案 1 :(得分:1)
尝试以这种方式书写,下面是在目标-C
中设置按钮动画的简单示例[UIButton animateWithDuration:0.5 animations:^{
self.horizontalConstraint.constant+=150;
[self.checkbut layoutIfNeeded];
} completion:^(BOOL finished) {
}];
您的代码
func constraintAnimation (duration: Double, animatedConstraint: NSLayoutConstraint, finalConstantValue: CGFloat) {
UIImageView.animateWithDuration(duration, animations: {
animatedConstraint.constant = finalConstantValue//Set the constant relative to initial positon like i have didi in above example
self.view.layoutIfNeeded()
})
println("")
}