2

I have a problem with the transition animation between two storyboard controllers. I have a singleView project with two view controllers in the storyboard.

Now I wand to make a custom transition animation:

  • my current view controller should disappear to the left
  • the new view controller should come from the right

I have already a UIStoryboardSegue class.

But what should I write in the -(void)perform{} method??

Thanks a lot,

Jonas.

djromero
  • 19,551
  • 4
  • 71
  • 68
Jonas Ester
  • 83
  • 2
  • 8
  • Just write "custom segue" in google and you will find many example. First do some try and than come here with what you have tried. one example : http://blog.jimjh.com/a-short-tutorial-on-custom-storyboard-segues.html – CRDave Oct 08 '13 at 09:16
  • And BTW what animation you want is default push segue animation you don't have to write code for that. – CRDave Oct 08 '13 at 09:17
  • 1
    @CRDave-- only if you want to use a UINavigationController. If you don't want to use that controller, then you have to custom code the same transition. Seems silly that xcode breaks mvc so hard (ie, that you can only get a certain transition if you have a certain controller), but there it is. – mmr Mar 21 '14 at 18:28

1 Answers1

11

For that simple segue you can try something like this:

- (void)perform {
    UIViewController* source = (UIViewController *)self.sourceViewController;
    UIViewController* destination = (UIViewController *)self.destinationViewController;

    CGRect sourceFrame = source.view.frame;
    sourceFrame.origin.x = -sourceFrame.size.width;

    CGRect destFrame = destination.view.frame;
    destFrame.origin.x = destination.view.frame.size.width;
    destination.view.frame = destFrame;

    destFrame.origin.x = 0;

    [source.view.superview addSubview:destination.view];

    [UIView animateWithDuration:1.0
                     animations:^{
                         source.view.frame = sourceFrame;
                         destination.view.frame = destFrame;
                     }
                     completion:^(BOOL finished) {
                         UIWindow *window = source.view.window;
                         [window setRootViewController:destination];
                     }];
}
Odrakir
  • 4,254
  • 1
  • 20
  • 52