1

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...

Community
  • 1
  • 1
Nerrolken
  • 1,975
  • 3
  • 24
  • 53

1 Answers1

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And see my book: http://www.apeth.com/iOSBook/ch17.html#_animation_and_autolayout – matt Jan 31 '15 at 02:40
  • This was the key, thanks! I disabled autolayout, and everything immediately started working. – Nerrolken Feb 02 '15 at 17:55
  • @Nerrolken That's not actually the solution I was thinking of. You need to learn to _use_ autolayout, not disable it. – matt Feb 02 '15 at 18:07
  • Autolayout is great for some situations, but it's not perfect every time for every need. I had created that XIB and forgotten to turn it off, but it was always my intention not to use it, for a number of reasons. – Nerrolken Feb 02 '15 at 18:30
  • @Nerrolken Okay, you win this time. :) And you're not really cheating. If you look carefully at Apple's own examples of animations, such as UIKit dynamics, you will see that they have cleverly turned off Autolayout. As I say in my book, Autolayout has conflicted with animations since it was introduced in iOS 6, and instead of addressing the issue, Apple has swept it under the rug ever since. – matt Feb 02 '15 at 18:33