dismissModalViewController with transition: left to right

jcdmb picture jcdmb · Jul 10, 2012 · Viewed 10.2k times · Source

I was using a nice method to dismiss my modal view controller:

[self dismissModalViewControllerWithTransition:2];

which makes a slide transition from left to right, like a navigation controller does to pop a view.

As this method is a non-public method, apple will not accept it. How can I program this kind of animation in my code (slide from left to right, to dismiss a modal view, and slide from right to left to present a modal view) ?

Thanks in advance

Answer

jcdmb picture jcdmb · Jul 10, 2012

I have accepted the answer from Safecase, but I would like to publish my final solution here:

1) To present a modal view controller with a from right to left transition I have written following method:

-(void) presentModalView:(UIViewController *)controller {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.35;
    transition.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromRight;

    // NSLog(@"%s: self.view.window=%@", _func_, self.view.window);
    UIView *containerView = self.view.window;
    [containerView.layer addAnimation:transition forKey:nil];
    [self presentModalViewController:controller animated:NO];
}

2) To dismiss a modal view with an slide transition left to right:

-(void) dismissMe {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.35;
    transition.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromLeft;

    // NSLog(@"%s: controller.view.window=%@", _func_, controller.view.window);
    UIView *containerView = self.view.window;
    [containerView.layer addAnimation:transition forKey:nil];

    [self dismissModalViewControllerAnimated:NO];
}

Thanks guys!