iosanimationuimodaltransitionstyle

Dissolve transition reverts UI element frame changes


I'm using a modal transition to shift from one XIB to another, and I've got it all working except for one thing: at the moment that the transition begins, all the movement animations I've done on the previous view are reverted.

Here's the method I'm working with:

- (IBAction)chooseInsight:(id)sender {
    [CATransaction setCompletionBlock:^{
        ContainerViewController *insight = [[ContainerViewController alloc] init];
        insight.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:insight animated:YES completion:nil];
    }];
    [self animateExit];
}

The animateExit method animates a 1-second frame movement for several UI objects, giving the effect that everything flies off the screen leaving a solid-colored background. My hope was that this solid color background would then dissolve into the next view, the ContainerViewController.

But what happens is that the UI objects fly off the screen, we see the solid color background, and then suddenly all the buttons and labels snap back so they can dissolve into the ContainerViewController.

Why is that happening? Has an image of the previous view been cached, to aid the animation? If so, can I refresh the cache before the transition? Or if not, what can I do to make this dissolve work smoothly?

Edit: In case it's relevant, I got the CATransaction bit from this answer about how to delay until the end of an animation. There's a voice in the back of my mind saying that maybe the two animations are the source of the problem, but I'm not familiar enough with iOS animations to figure out how...


Solution

  • all the movement animations I've done on the previous view are reverted.

    Because you performed those animations by changing the frames (or centers) of those subviews. But you are also using Autolayout. You can't do that. Frames and Autolayout are enemies to one another.

    When the transition comes along, layout happens. That means that the constraints are obeyed - that is what Autolayout means. But you did not change the constraints (which is what you should have done); you changed the frames. The constraints win, so everything goes back to where it was, because that is what the constraints say to do.