带动画块的NSLayoutConstraint

时间:2014-09-07 16:46:32

标签: objective-c animation autolayout

我有动画和NSLayout的问题,常量不是第一次更新和。仅在第二次采用正确的值。

这是我的代码:

if(gb == self.graphBar0){
                         self.gb0HeightConstraint.constant = newConstant;
                         [self.graphBar0 setNeedsUpdateConstraints];
                         [self.graphLabel0 setNeedsUpdateConstraints];
                     }else if(gb == self.graphBar1){
                         self.gb1HeightConstraint.constant = newConstant;
                         [self.graphBar1 setNeedsUpdateConstraints];
                         [self.graphLabel1 setNeedsUpdateConstraints];
                     }else if(gb == self.graphBar2){
                         self.gb2HeightConstraint.constant = newConstant;
                         [self.graphBar2 setNeedsUpdateConstraints];
                         [self.graphLabel2 setNeedsUpdateConstraints];
                     }

 [UIView animateWithDuration:duration
                      delay:0.0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     if(gb == self.graphBar0){

                         [self.graphBar0 layoutIfNeeded];
                         [self.graphLabel0 layoutIfNeeded];
                     }else if(gb == self.graphBar1){

                         [self.graphBar1 layoutIfNeeded];
                         [self.graphLabel1 layoutIfNeeded];
                     }else if(gb == self.graphBar2){

                         [self.graphBar2 layoutIfNeeded];
                         [self.graphLabel2 layoutIfNeeded];
                     }
                     [gb setBackgroundColor:newBarColor];
                 }
                 completion:^(BOOL finished) {
                     if(gb == self.graphBar0){
                         self.oldConstant0 = newConstant;
                     }
                     if(gb == self.graphBar1){
                         self.oldConstant1 = newConstant;
                     }
                     if(gb == self.graphBar2){
                         self.oldConstant2 = newConstant;
                     }
                 }];

有什么我想念的吗?

谢谢, 弥撒

1 个答案:

答案 0 :(得分:0)

在动画块之前将setNeedsUpdateConstraints更改为layoutIfNeeded

更新动画中的约束而不是完成块,然后立即调用layoutIfNeeded