Search code examples
iosswiftxcodeuiviewcontrolleruiviewanimationtransition

Custom View Controller Transitions xcode8


I'm trying to make custom view transition following this tutorial. Here is my code

class ItemsTableViewController: UITableViewController, UIViewControllerTransitioningDelegate {

let customPresentAnimationController = CustomPresentAnimationController()

// viewDidLoad and TableView methods

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showAction" {
        let toViewController = segue.destination as UIViewController
        toViewController.transitioningDelegate = self
        toViewController.modalPresentationStyle = .custom
    }
}

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return customPresentAnimationController
}

}

and for CustomPresentAnimationController

class CustomPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 5
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
    let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
    let finalFrameForVC = transitionContext.finalFrame(for: toViewController)
    let containerView = transitionContext.containerView
    let bounds = UIScreen.main.bounds
    toViewController.view.frame = finalFrameForVC.offsetBy(dx: 0, dy: bounds.size.height)
    containerView.addSubview(toViewController.view)

    UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: .curveLinear, animations: {
        fromViewController.view.alpha = 0.5
        toViewController.view.frame = finalFrameForVC
        }, completion: {
            finished in
            transitionContext.completeTransition(true)
            fromViewController.view.alpha = 1.0
    })
}

}

However the custom transition does not work. The problem is that the method animationControllerForPresentedController is not called. How can I fix this?


Solution

  • In swift 3 the method

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    return customPresentAnimationController
    }
    

    have been replaced to

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return customPresentAnimationController
    }