使用TransitionManager在控制器之间导航

时间:2015-05-24 19:28:30

标签: ios iphone swift uiviewanimation transitions

我想从第二个控制器导航到第一个控制器,不使用segue,而是将@IBAction连接到我的后退按钮,下面是此方法的代码:

let deviceStoryboard = UIStoryboard(name: "Storyboard", bundle: nil)
let deviceDashView = deviceStoryboard.instantiateViewControllerWithIdentifier("FirstView") as! 
self.navigationController!.transitioningDelegate = self.transitionManager
self.navigationController!.pushViewController(deviceDashView, animated: true)

正如您所看到的,我正在尝试附加自己的TransitionManager。我之所以创建它,是因为我希望有一个过渡动画,例如“从左到右”,默认情况下它是相反的。

如果我使用segue更改视图,我知道如何附加我的TransitionManager,但尝试了navigation controller的不同配置,但似乎没有任何配置。

我在哪里犯错?

1 个答案:

答案 0 :(得分:0)

这有点复杂,你需要的是首先获取你想要转换的视图控制器并在点击按钮时设置如下:

var presenterStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = presenterStoryBoard.instantiateViewControllerWithIdentifier("LoginViewController") as? LoginViewController
{
    self.isPresenting = false //this flag helps to find the end of the transition
    vc.modalPresentationStyle = .Custom
    vc.transitioningDelegate = self
    self.presentViewController(vc , animated: true)
    {
        self.isPresenting = false
     }
}

之后你需要创建一个负责动画的类,这个类实现了UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate并且具有转换所必需的方法:

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate  {

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

        // get reference to our fromView, toView and the container view that we should perform the transition in
        let container = transitionContext.containerView()
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: nil, animations: {
                //Create your animation here
            }, completion: { 
                finished in
                transitionContext.completeTransition(true)
        })
}

// return how many seconds the transition animation will take
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 0.5
}

// return the animataor when presenting a viewcontroller
// remmeber that an animator (or animation controller) is any object that aheres to the UIViewControllerAnimatedTransitioning protocol
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
}

// return the animator used when dismissing from a viewcontroller
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

请记住,从主人到详细信息来自和依赖的方向的视图是Master到详细信息,但是当你忽略它的细节时它会反转,而from将是详细信息,而to将是