user2851943
user2851943

Reputation: 860

Animation not always visible from animateTransition in UIViewControllerAnimatedTransitioning

I am using some UIViewControllers contained in a UINavigationController.

One is presented with a push segue from a UIButton on the storyboard, and is then dismissed using a swipe gesture which calls

popViewControllerAnimated

I'm using a UINavigationControllerDelegate to provide a custom object which conforms to UINavigationControllerDelegate. The code for animateTransition is shown below.

My problem is that the first time this runs, the view animates in when presenting, but every time after that, it doesn't animate (it just appears instantly).

Can anyone help?

Thanks!

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

//Get references to the view hierarchy
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];


if (self.isPresenting) {

    [containerView insertSubview:toViewController.view belowSubview:fromViewController.view];

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromViewController.view.transform = CGAffineTransformMakeTranslation(-1000, 0);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

} else if (!self.isPresenting) {
    //Add 'to' view to the hierarchy
    [containerView insertSubview:toViewController.view belowSubview:fromViewController.view];

    //Scale the 'from' view down until it disappears
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        //toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        fromViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}
}

Upvotes: 0

Views: 1394

Answers (2)

Chen Lim
Chen Lim

Reputation: 758

I ran into a similar set of issues after upgrading to iOS8. A few things to check:

  1. Make sure to call these before starting the animations:

    [toViewController beginAppearanceTransition:YES animated:YES]; [fromViewController beginAppearanceTransition:NO animated:YES];

  2. Then call these in the completion block:

    [toViewController endAppearanceTransition]; [fromViewController endAppearanceTransition]; [transitionContext completeTransition:finished];

The above is needed to ensure viewWillAppear: and viewWillDisappear: are called at the right times.

  1. Set modalPresentationStyle = UIModalPresentationFullScreen, instead of UIModalPresentationCustom (which behaves very differently).

Hope this helps.

Cheers!

Upvotes: 2

Lanston
Lanston

Reputation: 11854

Be careful setting self.navigationController.delegate

you probably place it in somewhere that only runs once like ViewDidLoad
try placing it in ViewDidAppear

    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        self.navigationController.delegate = self
    }

Upvotes: 1

Related Questions