The presented view controller disappears after animating using custom animations UIViewController

I've been looking for an answer to this and have spent the last two hours pulling my hair out of the end.

I am implementing a very basic custom view controller transition animation that just scales in the view of the view controller and grows in the presented view controller. It adds a fade effect (0 to 1 alpha and vice versa).

It works fine when presenting a view controller, however, when fired, it brings the view controller presenting

back to fill the screen, but then it inexplicably disappears. I don't do anything after these animations to change alpha or hidden values, this is a fairly new project. I have been developing iOS apps for 3 years, so I suspect it might be a bug if someone doesn't know where I am going wrong.

class FadeAndGrowAnimationController : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController!, presentingController presenting: UIViewController!, sourceController source: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
    return self
}

func animationControllerForDismissedController(dismissed: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
    return self
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {
    return 2
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as UIViewController
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as UIViewController

    toViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
    toViewController.view.alpha = 0

    transitionContext.containerView().addSubview(fromViewController.view)
    transitionContext.containerView().addSubview(toViewController.view)
    transitionContext.containerView().bringSubviewToFront(toViewController.view)

    UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
        fromViewController.view.transform = CGAffineTransformScale(fromViewController.view.transform, 2, 2)
        fromViewController.view.alpha = 1

        toViewController.view.transform = CGAffineTransformMakeScale(1, 1)
        toViewController.view.alpha = 1
    }, completion: { finished in
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    })
}

      

}

And the code for the view:

    let targetViewController = self.storyboard.instantiateViewControllerWithIdentifier("Level1ViewController") as Level1ViewController
    let td = FadeAndGrowAnimationController()

    targetViewController.transitioningDelegate = td
    targetViewController.modalPresentationStyle = .Custom

    self.presentViewController(targetViewController, animated: true, completion: nil)

      

As you can see, the animation is pretty simple. Am I missing something? As I said, it looks great and then throws away 99.99% just fine, but the view manager after being fired is inexplicably removed. The iPad shows a blank screen - completely black - after that happens.

+3


source to share


2 answers


It seems to be an iOS8 bug. I found a solution, but it's ghetto. After the transition, when the view should be displayed on the screen but is not, it needs to be added back to the window as follows:

BOOL canceled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!canceled];
if (!canceled)
    {
    [[UIApplication sharedApplication].keyWindow addSubview: toViewController.view];
    }

      



You may have to play around with which view you add back to the window, whether you do it in canceled or canceled, and perhaps make sure you only do this when you leave and not in your presentation.

Sources: Container view fading out at fullTransition: http://joystate.wordpress.com/2014/09/02/ios8-and-custom-uiviewcontrollers-transitions/

+4


source


had the same ios 8.1 problem

my quick code after completeTransition



if let window = UIApplication.sharedApplication().keyWindow {
    if let viewController = window.rootViewController {
        window.addSubview(viewController.view)
    }
}

      

-1


source







All Articles