Joey Yi Zhao
Joey Yi Zhao

Reputation: 42500

How to change the direction of animation when navigation pops a view controller

I want to change the direction when I pop a view controller. Below is the code I am using. I want to pop the controller from right (just a opposite direction when push a view controller) but it doesn't work. It just fade out gradually. How can I make the animation to be fading out from the right?

let transition = CATransition()
        transition.duration = 0.3
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        transition.type = kCATransitionFromRight

        self.navigationController?.view.layer.addAnimation(transition, forKey: nil)
        self.navigationController?.popToRootViewControllerAnimated(false)

Upvotes: 1

Views: 445

Answers (1)

nishith Singh
nishith Singh

Reputation: 2998

Try using SOLPresentingFun library

Sample code:

func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?{
    switch operation{
    case .Push where toVC.isKindOfClass("Your new View Controller class name"):
        let animator = SOLSlideTransitionAnimator()
        animator.appearing = true
        animator.duration = 0.25
        animator.edge = .Left //Direction from which you want to slide the new controller in
        return animator
    case .Pop where fromVC.isKindOfClass("Your new View Controller class name"):
        let animator = SOLSlideTransitionAnimator()
        animator.appearing = false
        animator.duration = 0.25
        animator.edge = .Left//Direction to which you want to slide the new controller out
        return animator
     }
    return nil
    }

Of course you have to set your base controller as the navigation controller delegate

self.navigationController?.delegate = self

and confirm that your base controller implements UINavigationControllerDelegate

Upvotes: 1

Related Questions