I am using custom view controller transitions, UIViewControllerAnimatedTransitioning
, to present and dismiss a view controller.
The presenting animation works fine, but when I run the dismiss animation, once I call completeTransition:
the containerView
gets removed.
I'm not sure what is going on, here is the transition code:
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
containerView.backgroundColor = [UIColor blackColor];
if (self.reverse) {
[containerView addSubview:toViewController.view];
[containerView addSubview:fromViewController.view];
} else {
[containerView addSubview:fromViewController.view];
[containerView addSubview:toViewController.view];
}
if (! self.reverse) { // Forward
toViewController.view.frame = CGRectMake(-containerView.frame.size.width, 0, containerView.frame.size.width, containerView.frame.size.height);
} else {
fromViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
}
[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveLinear animations:^{
if (self.reverse) {
fromViewController.view.frame = CGRectMake(-containerView.frame.size.width, 0, containerView.frame.size.width, containerView.frame.size.height);
fromViewController.view.layer.opacity = 0.f;
toViewController.view.layer.opacity = 1.f;
} else {
toViewController.view.frame = CGRectMake(0, 0, containerView.frame.size.width, containerView.frame.size.height);
toViewController.view.layer.opacity = 1.f;
fromViewController.view.layer.opacity = 0.3f;
}
} completion:^(BOOL finished) {
[transitionContext completeTransition:finished];
}];
}
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
if (self.reverse) {
return 0.45;
} else {
return 0.35;
}
}
How can I prevent my toViewController
from disappearing if .reverse
is set to YES
?
Update: This is how I'm presenting the view controller:
SecondaryViewController *vc = [[SecondaryViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.modalPresentationStyle = UIModalPresentationCustom;
navigationController.transitioningDelegate = self;
[self presentViewController:navigationController animated:YES completion:nil];
toViewController
from disappearing. – Keos