Xcode custom segue animation

2019-04-01 17:27发布

问题:

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.

回答1:

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];
                     }];
}