0

I have following simple nested animation code intended for transition as in the attached image.

[UIView animateWithDuration:2.0f animations:^{
    CGRect frame = self.label.frame;
    frame.origin.y += 100;
    self.label.frame = frame;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:2.0f animations:^{
        self.button.transform = CGAffineTransformMakeRotation(M_PI);
    }completion:^(BOOL finished) {
        //
    }];
}];

intended animation transition

The view layout is defined in XIB file with auto layout. As IB automatically puts it, it has vertical constraint on both button and label.

In this case, the transition goes not as depicted in the attached image. It goes from State1 to State2, but goes back to State1 briefly and then goes to State3.

What I figured is that this is caused by the vertical constraint of the label. As I programmatically removed the constraint in before the animation, the animation started working as intended.

[self.view removeConstraint:self.labelConstraint];

What I want to know is if this is the correct way to do it. Some post suggests that removing constraint sometimes causes unpredicted behavior due to insufficient constraints to determine layout.

4

1 回答 1

0

Okay, my friend answered my question which made complete sense to me.

[UIView animateWithDuration:2.0f animations:^{
    CGRect frame = self.label.frame;
    frame.origin.y += 100;
    self.label.frame = frame;
} completion:^(BOOL finished) {

    ////////////////////////////////////
    self.labelConstraint.constant += 100;
    ////////////////////////////////////

    [UIView animateWithDuration:2.0f animations:^{
        self.button.transform = CGAffineTransformMakeRotation(M_PI);
    }completion:^(BOOL finished) {
        //
    }];
}];

Note that I am changing the constraint value to suit to the new frame, before the second animation starts. This way there will be sufficient auto layout.

Still waiting for other ways if someone have them!

于 2013-04-11T15:59:29.933 回答