导航弹出视图控制器时如何更改动画的方向

时间:2016-04-28 07:14:13

标签: ios swift

我想在弹出视图控制器时改变方向。以下是我正在使用的代码。我想从右侧弹出控制器(当推动视图控制器时只是相反的方向),但它不起作用。它逐渐淡出。如何让动画从右边淡出?

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)

1 个答案:

答案 0 :(得分:1)

尝试使用SOLPresentingFun

示例代码:

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
    }

当然,您必须将基本控制器设置为导航控制器委托

self.navigationController?.delegate = self

并确认您的基本控制器实现了UINavigationControllerDelegate