约束动画不起作用Swift

时间:2015-05-07 02:41:09

标签: ios swift animation uiviewanimation

我正在尝试为UIImageView制作动画,基本上在整个视图在屏幕上的原始位置。只要我按下一个按钮,视图就会向左滑动,只能看到它的一部分。 constraint.constant的值为:

  1. 屏幕位置= 0(这是起始位置
  2. 关闭屏幕位置= -200(应该是到达点)
  3. 这是代码。

    func constraintAnimation (duration: Double, animatedConstraint: NSLayoutConstraint, finalConstantValue: CGFloat) {
    
        self.view.layoutIfNeeded()
        UIImageView.animateWithDuration(duration, animations: {
        animatedConstraint.constant = finalConstantValue
    
        })
        self.view.layoutIfNeeded()
        println("")
    
    }
    

2 个答案:

答案 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("")

}