Dailycode.info

Short solution for short problems

iOS : Custom Flip Segue (portrait and landscape layout) xcode 4

Using segues is really easy. Get use to the visual programming! If you draw segues its still a shame that you cannot visual change the animation on the segues. But then the implementation of a custom animation is not so hard.

First create a new Objective C class and give it a usefull name. For example: FlipSegue. 

This class should inherit from the UIStoryboardSegue.

#import <UIKit/UIKit.h>

 

@interface FlipSegue : UIStoryboardSegue

 

@end

Then in the implementation file we override the perform method. Below you can see that I do a check if the orientation is landscape or portrait, because then you will always flip from left to right. If not, when you change orientation, it will flip from top to bottom. Here is the full code for the custom segue implementation.

#import "FlipSegue.h"

 

@implementation FlipSegue

- (void)perform

{

    UIInterfaceOrientation orientation = [self.sourceViewControllerinterfaceOrientation];

    if (UIInterfaceOrientationIsPortrait(orientation))

    {

        UIViewController *src = (UIViewController *) self.sourceViewController;

        UIViewController *dst = (UIViewController *) self.destinationViewController;

        [UIViewtransitionWithView:src.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromLeft

                    animations:^{

                        [src.navigationController pushViewController:dst animated:NO];

                    }

                    completion:NULL];

    }

    else

    {

        UIViewController *src = (UIViewController *) self.sourceViewController;

        UIViewController *dst = (UIViewController *) self.destinationViewController;

        [UIViewtransitionWithView:src.navigationController.view duration:0.8 options:UIViewAnimationOptionTransitionFlipFromBottom

                        animations:^{

                            [src.navigationController pushViewController:dst animated:NO];

                        }

                        completion:NULL];

    }

}

 

@end

Now the last thing we need to do is tell the Segue that it has to inherit from this class: