1
votes

I have a UIScrollView placed inside of a ViewController to which I have given AutoLayout Constraints and all of them are satisfied. The scrollView contains around 20 UIButton subviews. I have given autolayout constraints to them also and they all are also satisfied.

What I need to do is I want to animate those buttons when the view is first launched. This is my code for animation

    for (UIButton *button in [scrollView subviews]) {
    CGRect mainFrame = [button frame];
    [UIView animateWithDuration:0.0 animations:^{
        button.frame = CGRectMake(button.center.x, button.center.y, 0, 0);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 delay:0.5 usingSpringWithDamping:0.3 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
            button.frame = mainFrame;
        } completion:^(BOOL finished) {
        }];
    }];

}

The subviews are not animating when I use this code. But when I set the

[button setTranslatesAutoresizingMaskIntoConstraints:YES];

it works. But this time I get the error Unable to Simultaneously Satisfy constraints.

Is there any other method to do it or should I just ignore the error message and just go with it?

2

2 Answers

3
votes

Don't modify the frames when using Auto Layout.

You should modify the constraints to achieve the animation and call [yourView layoutIfNeeded] in the animation block.

It works by setting setTranslatesAutoresizingMaskIntoConstraints because then setting the frame causes constraints to be created. These finally conflict with your already existing constraints and cause the constraint warning.

Alternatively you can use the view's transform property and animate that one to achieve your desired effect, since I think you only want something like a presentation animation. Then you don't have to modify the constraints. You would do something like:

[UIView animateWithDuration:1.0 animations:^{
  button.transform = CGAffineTransformMakeTranslation(10.0, 10.0);
}];
0
votes

You should animate the values for the constants in the constraints instead of the frame for each button. Create internal properties for each constraint you would like to animate and then use UIView's animateWithDuration. For example:

self.myConstraint.constant = 10;
[button setNeedsUpdateConstraints];
[UIView animateWithDuration:1 animations:^{
  [button layoutIfNeeded];
}];