Custom Transition

Navigation Controller에 올라탄 view controller들의 transition 효과를 커스텀해본다.

Navigation Controller Delegate

import UIKit
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        if fromVC.isKind(of: StartingController.self) {
            return ShowTransition()
        }else if fromVC.isKind(of: EndingController.self) {
            return DismissTransition()
        }else {
            return nil
        }
        
    }
}

Custom Transition

class ShowTransition: NSObject, UIViewControllerAnimatedTransitioning 
class DismissTransition: NSObject, UIViewControllerAnimatedTransitioning 
func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
// sample
guard
    let sourceViewController = transitionContext.viewController(forKey: .from),
    let destinationController = transitionContext.viewController(forKey: .to),
    let destinationView = destinationController.view,
    else { return }
    
let containerView = transitionContext.containerView
containerView.addSubview(destinationView)