5

I have an UINavigationController. I made VC1 the rootViewController and programatically load VC2 from VC1 and then have the custom animation to go from VC1 to VC2. Standard. Everything is fine and good.

Now, I would like to have a custom animation between the two like so: Custom animation

In sum, VC1 slides out of view while VC2 is beneath it. Just like a stack of paper where you slide away the first sheet (VC1) and thus reveal the sheet beneath (VC2).

So what I tried is the following code which is called from VC1 in order to get to VC2. But there are problems with it:

MyVC2 *vctwo = [[[MyVC2 alloc] init] autorelease];

CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[[self navigationController] pushViewController:vctwo animated:YES];

The problems are the following:

  1. VC2 is not fixed in the background. It kind of slides in as well (even though I specified kCATransitionReveal). I want to be VC2 totally fixed in the background.
  2. VC1 fades out. I don't really know why. I did not use kCATransitionFade or the like, so I can't see where the fade comes from.

Any suggestions why I am not getting the expected results would be very much appreciated. Sorry if this is obvious, but I am trying this for hours now and got really frustrated.

n.evermind
  • 11,944
  • 19
  • 78
  • 122

2 Answers2

3

I think you should use [[self navigationController] pushViewController:vctwo animated:NO]; rather than animated:YES.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Thanks Ole, I changed it and VC2 doesn't move anymore. Should have thought about it. Any thought about the fade, though? I still can't see where the fade out animation is coming from. – n.evermind Oct 05 '11 at 18:16
  • I think the fade is actually part and parcel of the UINavigationController animation and I can't seem to get rid of it. Do you know of any way to disable the custom fade animation the UINavigationController implements? – n.evermind Oct 05 '11 at 19:52
1

I suppose the only way to do this is to forget UINavigationController and come up with my own mechanism to handle everything:

This blog explains how...

So if you wanted to custom animations between VCs, do not use UINavigationController. Here is sample code to swap between two VCs as described above:

    -(void)slideDoorOpenTo:(UIViewController *)aController duration:(float)aDuration {

    [aController viewWillAppear:YES];
    [activeController viewWillDisappear:YES];

    //aController.view.alpha = 0.0f;
    [self.view insertSubview:aController.view belowSubview:activeController.view]; // so that it is below activeController

    [aController viewDidAppear:YES];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:aDuration];

    aController.view.transform = CGAffineTransformMakeTranslation(0,0);
    activeController.view.transform = CGAffineTransformMakeTranslation(-320,0);

    [UIView commitAnimations];

    [self performSelector:@selector(animationDone:) withObject:aController afterDelay:aDuration];


}
n.evermind
  • 11,944
  • 19
  • 78
  • 122