Mistre83
Mistre83

Reputation: 2827

iOS 7 - Modal popup with UIViewcontrollerAnimatedTransitioning

i'm trying to present PopUp ViewController with animation, using UIViewcontrollerAnimatedTransitioning. I've created a Modal Segue from TableViewCell to my Viewcontroller

In PopupPresentAnimationController (that implement UIViewcontrollerAnimatedTransitioning) i have

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

    [fromViewController addChildViewController:toViewController];
    toViewController.view.frame = fromViewController.view.frame;
    [fromViewController.view addSubview:toViewController.view];

    [toViewController didMoveToParentViewController:fromViewController];

    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        fromViewController.view.alpha = 0.5;
    } completion:^(BOOL finished) {
        fromViewController.view.alpha = 1.0;
        [transitionContext completeTransition:YES];
    }];
}

The PopUpViewController have a background black with 50% opacity and, when it appear all "works" but after animation end, the screen become black.

UPDATE 1:

UIViewController *fromViewController =[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *container = [transitionContext containerView];

    CGRect initialFrame = toViewController.view.frame;
    initialFrame.origin.y = toViewController.view.frame.size.height;

    toViewController.view.frame = initialFrame;
    [container insertSubview:toViewController.view aboveSubview:fromViewController.view];

    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:duration delay:0 options:0 animations:^{
        CGRect newFrame = toViewController.view.frame;
        newFrame.origin.y = 0;
        toViewController.view.frame = newFrame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

UPDATE 2 In prepareForSegue i have added the follow line

controller.modalPresentationStyle = UIModalPresentationCustom; // controller is the destination

With this line on, the black is no more black!

Upvotes: 2

Views: 1961

Answers (1)

KerrM
KerrM

Reputation: 5240

The problem you're having is that you are adding your toViewController to the fromViewController. When the animation ends, the toViewController is removed and in turn, so is your fromViewController. The proper way to handle this is to use the container view provided by the context:

UIView *container = [transitionContext containerView];

This is an example of how to do a popup transition:

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
  UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  UIView *container = [transitionContext containerView];

  if (!self.beingDismissed) {
    //Make controller hidden so it can slide in
    CGRect initialFrame = toViewController.view.frame;
    initialFrame.origin.y = toViewController.view.frame.size.height;

    toViewController.view.frame = initialFrame;
    [container insertSubview:toViewController.view aboveSubview:fromViewController.view];
  }
  [UIView animateKeyframesWithDuration:0.5 delay:0 options:0 animations:^{
    if (!self.beingDismissed) {
        //Show view controller
        CGRect newFrame = toViewController.view.frame;
        newFrame.origin.y = 0;
        toViewController.view.frame = newFrame;
    } else {
        //Hide view controller
        CGRect newFrame = fromViewController.view.frame;
        newFrame.origin.y = fromViewController.view.frame.size.height;
        fromViewController.view.frame = newFrame;
    }
  } completion:^(BOOL finished) {
    [transitionContext completeTransition:finished];
  }];
}

Upvotes: 2

Related Questions