I am trying to use UIViewController
's transitionFromViewController:toViewController:duration method but with a custom animation.
I have the following two view controllers added as children to a custom container UIViewController:
The following code works as expected:
[self transitionFromViewController:firstController
toViewController:secondController
duration:2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^(void){}
completion:^(BOOL finished){}];
However I would like to create a custom animation where the where firstController
slides to the left and is replaced by secondController
sliding in from the right similar to how UINavigationControllers push and pop methods work. After changing the options
to UIViewAnimationOptionTransitionNone
I have tried to implement custom animations in the animations
block but have had absolutely no success. firstController
is immediately swapped for secondController
without and animations.
I would really appreciate any help.
Thank you
This is actually really easy. For some reason I assumed that the secondController
's view would be be under/ behind that of firstController
's. I had only tried to animate the firstController
's view. This of course is wrong. As soon as transitionFromViewController:toViewController:duration
is called secondController
's view is placed over firstController
's view. The following code works:
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
secondController.view.frame = CGRectMake(width, 0, width, height);
[self transitionFromViewController:firstController
toViewController:secondController
duration:0.4
options:UIViewAnimationOptionTransitionNone
animations:^(void) {
firstController.view.frame = CGRectMake(0 - width, 0, width, height);
secondController.view.frame = CGRectMake(0, 0, width, height);
}
completion:^(BOOL finished){
[secondController didMoveToParentViewController:self];
}
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With