Customise UINavigationController animation: CATran

2019-05-01 04:32发布

问题:

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:

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.

回答1:

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



回答2:

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];


}