Question

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;
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top