Vibha R Kedilaya
Vibha R Kedilaya

Reputation: 57

I need custom segue when dismissing view controller

I want to dismiss present viewController and I need custom segue to implement that.

- (void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UIViewController *sourceTabBarController = sourceViewController.parentViewController.parentViewController;
    UIViewController *destinationViewController = self.destinationViewController;
    UIGraphicsBeginImageContext(destinationViewController.view.bounds.size);
    [destinationViewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *destinationViewControllerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *destinationViewControllerImageView = [[UIImageView alloc] initWithImage:destinationViewControllerImage];
    destinationViewControllerImageView.userInteractionEnabled = YES;
    destinationViewControllerImageView.frame = CGRectMake(0.0f, 0.0f, CGRectGetWidth(destinationViewController.view.frame), CGRectGetHeight(destinationViewController.view.frame));

    [destinationViewController.view insertSubview:destinationViewControllerImageView atIndex:1];

    // Add animations
    [UIView animateWithDuration:0.4f
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         destinationViewControllerImageView.center = CGPointMake(-CGRectGetWidth(destinationViewControllerImageView.frame) / 2, -(CGRectGetHeight(destinationViewControllerImageView.frame) / 2));
                     }
                     completion:^(BOOL finished){
                         [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];
                     }];


}

It works fine but with no animation :(

Upvotes: 0

Views: 208

Answers (2)

Unheilig
Unheilig

Reputation: 16302

The problem is not because of setting the animated: to NO, rather it is because the animation is being interrupted; hence, presentViewController within the completion block is called immediately.

Instead, try wrapping your presentViewController: code within a if-clause like so:

if(finished)
{
     [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];
}

Since your animation duration is only 0.4f, you wouldn't really see it, because it happens so fast. Try setting the duration longer, then you would be able to see your destinationViewControllerImageView animates to its position before presentViewController is called.

I ran a quick test with animating opacity; it should work as expected.

Hope this helps.

Upvotes: 0

Ankit Kumar
Ankit Kumar

Reputation: 280

you are setting animated to NO here - may be problem is here

[[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:nil];

set it to animated:YES

Upvotes: 1

Related Questions