iosobjective-ciphoneseguecustom-transition

Call custom transition animator using a programmatically created segue IOS


the project I am working on needs to have a custom transition between two view controllers. I have setup and implemented UIViewControllerTransitioningDelegate by overriding the method like:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                              animationControllerForOperation:(UINavigationControllerOperation)operation
                                           fromViewController:(UIViewController *)fromVC
                                             toViewController:(UIViewController *)toVC
{
   //Creation of animation
   id<UIViewControllerAnimatedTransitioning> animationController;
   MyAnimator *animator = [[MyAnimator alloc] init];
   animator.appearing = YES;
   animationController = animator;
   return animator
}

And I have setup MyAnimator subclass to correctly implement UIViewControllerAnimatedTransitioning by overriding the method:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    //custom transition animation
}

The segue which I using to trigger this animation transition is created and called programmatically, like:

UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"showCardDetail" source:self destination:vc];
[self prepareForSegue:segue sender:self];
[segue perform];

Reason of creating segue programmatically: I have from(transition from) and to(transition to) view controllers in separate storyboards.

Problem / Question: I have seen many examples, all handle the custom transition using a storyboard segue like:

[self performSegueWithIdentifier:@"storyboardSegue-id" sender:self]

How should i define in the method -perform for segue created programmatically above so that the custom transition takes place.


Solution

  • You don't need to create a custom segue to do what you're trying to do. In fact, since you have your 2 controllers in separate storyboards, you shouldn't use a segue at all. Just instantiate your destination view controller, and use pushViewController:animated: to initiate the transition. Your first view controller should be the delegate of the navigation controller, so the delegate method you show at the top of your question gets called.