DCMaxxx
DCMaxxx

Reputation: 2574

Custom iOS navigation controller animation : is this wrong?

I've been asked to add a fully custom transition between two UIViewControllers and came up with this solution. It works, but I don't know if there's a better / more elegant way to do it.

By fully custom, I mean involving modifying the first view controller subviews (moving them, not only fading or whatever), being able to change the duration, etc.

I'd like to know two things :

As a few lines of code is better than a thousand words, here is a very simple example to get the idea :

// Considering that self.mainView is a direct subview of self.view
// with exact same bounds, and it contains all the subviews

DCSomeViewController *nextViewController = [DCSomeViewController new];
UIView *nextView = nextViewController.view;

// Add the next view in self.view, below self.mainView
[self.view addSubview:newView];
[self.view sendSubviewToBack:newView];

[UIView animateWithDuration:.75f animations:^{
    // Do any animations on self.mainView, as nextView is behind, you can fade, send self.mainView to wherever you want, change its scale...
    self.mainView.transform = CGAffineTransformMakeScale(0, 0);

} completion:^(BOOL finished) {
    // Push the nextViewController, without animation
    [self.navigationController pushViewController:vc animated:NO];

    // Restore old 
    self.backgroundImageView.transform = CGAffineTransformIdentity;
}];

I've double checked a few things that I thought might get wrong :

Thanks for your opinions.

Upvotes: 2

Views: 1925

Answers (1)

VivienCormier
VivienCormier

Reputation: 1133

There are differents way to make a transition between ViewControllers. Your code works but you can implement a more official way.

Use methode transitionFromViewController:toViewController: Check the tutorial : http://www.objc.io/issue-1/containment-view-controller.html#transitions

Or you can use UIViewControllerContextTransitioning Check the tutorials : http://www.objc.io/issue-5/view-controller-transitions.html and http://www.teehanlax.com/blog/custom-uiviewcontroller-transitions/

Upvotes: 2

Related Questions