1

I'm trying to create a custom transition for a modally presented view controller using UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning and UIPresentationController.

In my presenting view controller I implement UIViewControllerTransitioningDelegate and have the following methods:

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                              presentingController:(UIViewController *)presenting
                                                                  sourceController:(UIViewController *)source {

    return [[MyAnimationController alloc] initWithAnimationType:MyAnimationTypePresent];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[MyAnimationController alloc] initWithAnimationType:MyAnimationTypeDismiss];
}

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented
                                                  presentingViewController:(UIViewController *)presenting
                                                      sourceViewController:(UIViewController *)source {

    return [[MyPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

Now in my subclass of UIPresentationController I add a dimming view beneath the presented view controller and want to fade it in along with the appearance transition.

- (void)presentationTransitionWillBegin {
    self.dimmingView.alpha = 0.0f;
    [self.containerView insertSubview:self.dimmingView atIndex:0];
    [self.dimmingView autoPinEdgesToSuperviewEdges];

    [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.dimmingView.alpha = 1.0f;
    }
                                                                    completion:nil];
}

- (void)dismissalTransitionWillBegin {

    [self.presentedViewController.transitionCoordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.dimmingView.alpha = 0.0f;
    }
                                                                    completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
                                                                        [self.dimmingView removeFromSuperview];                                                                 }];
}

The interesting - and quite frustrating - thing is that the presentation and dismissal animations for my presented view controller work as expected and as implemented in MyAnimationController. As for the fade-in/out of my dimming view, it only works when dismissing the presented view controller. When presenting it, the fade-in is not animated alongside the transition but simply uses a fixed amount of time. I'm pretty sure I implemented everything according to Apple's documentation and several tutorials but for some reason it simply won't work as expected. Any suggestion as to what the issue might be here?

caryot
  • 251
  • 2
  • 5

1 Answers1

1

I believe you want to use the presentingViewController's transitionCoordinator on presentation, and presentedViewConroller's transitionCoordinator on dismissal. This is why it works on dismissal but not presentation as you have it currently implemented.

Procrastin8
  • 4,193
  • 12
  • 25