1

I am hoping you can help me I am trying to implement a Custom Transition in my app that will display a Custom Modal View Controller. I am able to get the View Controller to appear as expected, however, I cannot get the transition to animate. Any help with this or pointers in the right direction would be greatly appreciated.

Please see my code below:

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 1.0;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    CGRect sourceRect = [transitionContext initialFrameForViewController:fromVC];

    fromVC.view.frame = sourceRect;

    UIGraphicsBeginImageContextWithOptions(fromVC.view.frame.size, NO, 0);
    [fromVC.view drawViewHierarchyInRect:fromVC.view.bounds afterScreenUpdates:YES];
    UIImage *fromVCImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIView *container = [transitionContext containerView];
    [container setBackgroundColor:[UIColor colorWithPatternImage:fromVCImage]];

    [container insertSubview:toVC.view belowSubview:fromVC.view];

    CGRect fromVCFrame = fromVC.view.frame;

    __block CGRect toVCFrame = toVC.view.frame;
    toVCFrame.origin.y = fromVCFrame.origin.y + fromVCFrame.size.height;
    [toVC.view setFrame:toVCFrame];
    [toVC.view setAlpha:0.0];
    [toVC.view setBackgroundColor:[UIColor colorWithPatternImage:fromVCImage]];

    [transitionContext finalFrameForViewController:toVC];

    //3.Perform the animation...............................
    [UIView animateWithDuration:1.0
                     animations:^{
                         toVCFrame.origin.y = fromVCFrame.origin.y;
                         toVC.view.frame = toVCFrame;

                         [toVC.view setAlpha:1.0];
                     }
                     completion:^(BOOL finished) {
                         //When the animation is completed call completeTransition
                         [transitionContext completeTransition:YES];
                     }];
}

Adding Code from View Controller when presentViewController is called via a UITapGestureRecognizer

- (void) onTapGesture:(UITapGestureRecognizer *)recognizer
{
    ImagesCollectionViewController *imagesCollectionView = [[Utils getStoryboardForDeviceWithIdentifier:MAIN_STORYBOARD] instantiateViewControllerWithIdentifier:IMAGES_VIEW];
    imagesCollectionView.transitioningDelegate = self;
    imagesCollectionView.modalTransitionStyle = UIModalPresentationCustom;

    [self presentViewController:imagesCollectionView animated:YES completion:^{

    }];
}

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

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return self.contentSlideTransitionManager;
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Delete this line: `[transitionContext finalFrameForViewController:toVC];` It does nothing. Either that or set the your final frame to the returned value! – matt Feb 21 '14 at 01:20
  • The `__block` declaration is pointless and wrong. Get rid of it. – matt Feb 21 '14 at 01:20
  • Okay, so - is this code running at all? I'm wondering whether you are setting up the rest of the transition stuff correctly. Could you show the other methods? Prove that you are setting the `transitioningDelegate`. Prove that you are implementing `animationControllerForPresentedController:` correctly. – matt Feb 21 '14 at 01:21
  • Here's a working example; perhaps it will help. https://github.com/mattneub/custom-alert-view-iOS7/blob/master/CustomAlertViewDemo/CustomAlertViewController.m – matt Feb 21 '14 at 01:24
  • Hi Matt I had the __block declaration as I needed to change the Frame inside the animation block. The code does run and the View Controller is presented, unfortunately though it will just not animate the Transition. Are you aware of any reason why an animation would not show ? I have added the code called when calling presentViewController. Thank you for your help with this. Thanks, Michael – Michael Miscampbell Feb 21 '14 at 01:29
  • Can't you be bothered to format your code on stack overflow correctly? – matt Feb 21 '14 at 01:41
  • So *is* the code running? Is `self.contentSlideTransitionManager` a real object? Is its `animateTransition` actually being called? – matt Feb 21 '14 at 01:44
  • "I needed to change the Frame inside the animation block" Yes, but you don't need to set `toVCFrame.origin.y` inside the block, and you don't need a `__block` variable in order to _use_ `toVCFrame` inside the block. – matt Feb 21 '14 at 01:46
  • `[container setBackgroundColor:` Try getting rid of that line. You should not be doing anything to the container view itself. It isn't yours. – matt Feb 21 '14 at 01:49

1 Answers1

2

I'm going to guess that this is your problem:

 [container insertSubview:toVC.view belowSubview:fromVC.view];

If toVC.view is below fromVC.view, it is hidden behind it, so you are not seeing what it does.

matt
  • 515,959
  • 87
  • 875
  • 1,141