7

I'm trying to do a custom presentation animation but the VC is an MPMoviePlayerViewController. I was following this tutorial:

When I present the MPMoviePlayerViewController, all is right.

But when I dismiss it, simply the method:

 - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed

is not called, what I'm doing wrong?

This is the code:

- (IBAction)playButtonTouchedIn:(id)sender {

    NSURL *url = [[NSBundle mainBundle]URLForResource:@"Learn to speak Spanish quickly with funny videos on youtube" withExtension:@"mp4"];

    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    player.transitioningDelegate = self;
    player.modalTransitionStyle = UIModalPresentationCustom;
    [player.moviePlayer setShouldAutoplay:NO];
    player.moviePlayer.controlStyle = MPMovieControlStyleNone;
    [self presentViewController:player animated:YES completion:^{
        [player.moviePlayer play];
        player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    }];
}

The UIViewControllerTransitioningDelegate methods:

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:    (UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.presenting = YES;
    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.presenting = NO;
    return self;
}

The UIViewControllerAnimatedTransitioning Methods

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.7;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext     viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];


    if (self.isPresenting) {
        [fromViewController.view setUserInteractionEnabled:NO];

        [transitionContext.containerView addSubview:fromViewController.view];
        [transitionContext.containerView addSubview:toViewController.view];

        CGRect startFrame = toViewController.view.frame;
        toViewController.view.frame = (CGRect){CGPointMake(toViewController.view.frame.size.width, 0),startFrame.size};
        [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:4.0f options:UIViewAnimationOptionCurveEaseOut animations:^{    
            toViewController.view.frame = startFrame;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];

            [fromViewController.view setUserInteractionEnabled:YES];
        }];
    }
    else{
        [toViewController.view setUserInteractionEnabled:YES];

        [transitionContext.containerView addSubview:toViewController.view];
        [transitionContext.containerView addSubview:fromViewController.view];

        CGRect finalFrame = (CGRect){CGPointMake(-fromViewController.view.frame.size.width, 0),fromViewController.view.frame.size};
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            fromViewController.view.frame = finalFrame;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
}

I have tried with a view controller developed by me and it works great, so is should be the MPMoviePlayerViewController?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
xarly
  • 2,054
  • 4
  • 24
  • 40
  • 1
    did you find your solution? – Georg Apr 16 '15 at 10:13
  • This is happening to me too. Do you happen to remember if `MPMoviePlayerViewController` was inside of a `UINavigationController`? I'm wondering if that has something to do with it. – Matt Baker Oct 14 '15 at 15:09
  • Somewhat counterintuitively, it looks like you need to set `transitioningDelegate` and `modalPresentationStyle` AGAIN right before it dismisses. That was definitely not expected... – Matt Baker Oct 14 '15 at 15:54
  • @MattBaker where? Im having this problem, but not using any movie player api's – DevilInDisguise Oct 17 '15 at 09:15
  • @DevilInDisguise check out the answer I just posted and let me know if it resolves it for you. – Matt Baker Oct 27 '15 at 18:31

1 Answers1

0

The way I was able to get it to animate properly on dismissal was to re-set the transitioningDelegate and modalTransitionStyle right before calling [presentingViewController dismissViewControllerAnimated:YES];

So instead of just

- (IBAction) closePressed:(id)sender {
    [self.presentingViewController dismissViewControllerAnimated:YES];
}

You would do

- (IBAction) closePressed:(id)sender {
    MPCustomModalTransitionDelegate *delegate = [[MPCustomModalTransitionDelegate alloc]init];
    self.transitioningDelegate = delegate;
    self.modalTransitionStyle = UIModalPresentationCustom;
    [self.presentingViewController dismissViewControllerAnimated:YES];
}

The obvious difference here is the addition of a new MPCustomModalTransitionDelegate class, which you would need to refactor out of your view controller.

Matt Baker
  • 3,394
  • 3
  • 25
  • 35
  • I will try to have a look at it some time soon. I remember trying various stuff to get the stuff working. The main problem being that I could never get the custom transition to successfully and fully dismiss the VC, hence overloading memory heavily when swiping around. I did not find a fix for it. I did try calling dismissViewControllerAnimated and anything like, but that apparently can not force it to be deallocated; even thought it might make it disappear.. – DevilInDisguise Nov 06 '15 at 17:29