Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal flip direction in iOS Storyboards

I have set up two view controllers in my Storyboard, each with an action button. I set up two segues:

  1. From the first view controller to the second view controller: Modal, Flip Horizontal.
  2. From the second view controller to the first view controller: Modal, Flip Horizontal.

Everything works, but I would like it to flip one way and back, now it flips the same direction for both view controllers.

What is the easiest way to make a flip-flip back effect?

like image 484
Christoffer Avatar asked May 22 '12 11:05

Christoffer


3 Answers

I encountered the same problem, and hopefully found a solution.

I used a custom segue to flip the way back. Here is the code of my FlipLeftSegue

@implementation FlipLeftSegue
- (void)perform
{
    UIViewController *src = (UIViewController *) self.sourceViewController;
    UIViewController *dst = (UIViewController *) self.destinationViewController;    

    [UIView beginAnimations:@"LeftFlip" context:nil];
    [UIView setAnimationDuration:0.8];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:src.view.superview cache:YES];
    [UIView commitAnimations];

    [src presentViewController:dst animated:NO completion:nil];
}
@end

The only problem I found to this solution, is that the viewDidAppear method is called immediatly when the animation starts. Whereas with normal segue, it is called after the animation.

like image 73
NSZombie Avatar answered Nov 18 '22 12:11

NSZombie


Instead of creating a new presentation of a ViewController, I'd suggest accessing the property for presentingViewController on the second ViewController, and then calling -dismissViewControllerAnimated:completion: on it. Like so:

-(IBAction)someResponderToYourCancelButtonOrWhatever:(id)sender {
    // Do some stuff
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
like image 27
josefdlange Avatar answered Nov 18 '22 14:11

josefdlange


You better use this code in NSZombie's answer:

- (void)swapChildVCFrom:(UIViewController *)from to:(UIViewController *)to options:(enum UIViewAnimationOptions)options
{
    [self addChildViewController:to];
    [from willMoveToParentViewController:nil];

    // Adjust the new child view controller's view's frame
    // For example here just set it to fill the parent view
    to.view.frame = self.view.bounds;

    [self transitionFromViewController:from
                      toViewController:to
                              duration:1.0
                               options:options
                            animations:nil
                            completion:^(BOOL b)
                            {
                                [to didMoveToParentViewController:self];
                                [from.view removeFromSuperview];
                                [from removeFromParentViewController];
                            }];
}
like image 1
doozMen Avatar answered Nov 18 '22 13:11

doozMen