我正在尝试自定义演示动画,但VC是MPMoviePlayerViewController.我正在学习本教程:
当我呈现MPMoviePlayerViewController时,一切都是正确的.
但是当我解雇它时,只需要方法:
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
不叫,我做错了什么?
这是代码:
- (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; }]; }
UIViewControllerTransitioningDelegate方法:
- (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; }
UIViewControllerAnimatedTransitioning方法
- (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,fromViewController.view.frame.size}; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ fromViewController.view.frame = finalFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } }
我尝试过用我开发的视图控制器,它工作得很好,所以应该是MPMoviePlayerViewController吗?
解决方法
我在解雇时能够正确设置动画的方法是在调用[presentViewController dismissViewControllerAnimated:YES]之前重新设置transitioningDelegate和modalTransitionStyle;
而不仅仅是
- (IBAction) closePressed:(id)sender { [self.presentingViewController dismissViewControllerAnimated:YES]; }
你会的
- (IBAction) closePressed:(id)sender { MPCustomModalTransitionDelegate *delegate = [[MPCustomModalTransitionDelegate alloc]init]; self.transitioningDelegate = delegate; self.modalTransitionStyle = UIModalPresentationCustom; [self.presentingViewController dismissViewControllerAnimated:YES]; }
这里明显的区别是添加了一个新的MPCustomModalTransitionDelegate类,您需要从视图控制器中重构它.