互动转型:Segue声明

时间:2014-10-26 18:31:34

标签: ios swift segue interactive transitions

理论问题:在下面的代码中,为什么,在" performSegue ...."的行中,segue不是完全执行的?我的意思是,它是如何知道它是一个互动的过渡,以及为什么这个方法" performSegue ..."与她在其他地方宣布的效果不一样?

FirstViewController.swift

let transitionManager = TransitionManager()


override func viewDidLoad() {
    super.viewDidLoad()

    transitionManager.sourceViewController = self

    // Do any additional setup after loading the view.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    let dest = segue.destinationViewController as UIViewController
    dest.transitioningDelegate = transitionManager
    transitionManager.destViewController = dest

}

故事板

segue是从FirstViewController到SecondViewController。

  • 标识符:" segueIdentifier"

  • Segue:Modal

  • 目的地:当前

TransitionManager.swift

class TransitionManager: UIPercentDrivenInteractiveTransition,UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate,UIViewControllerInteractiveTransitioning {

    var interactive = false
    var presenting = false

    var panGesture : UIPanGestureRecognizer!

    var sourceViewController : UIViewController! {
        didSet {

            panGesture = UIPanGestureRecognizer(target: self, action: "gestureHandler:")
            sourceViewController.view.addGestureRecognizer(panGesture)

        }
    }


    func gestureHandler(pan : UIPanGestureRecognizer) {

        let translation = pan.translationInView(pan.view!)

        let d =  translation.x / pan.view!.bounds.width * 0.5        

        switch pan.state {

        case UIGestureRecognizerState.Began :

            interactive = true

            sourceViewController.performSegueWithIdentifier("segueIdentifier", sender: self)


        case UIGestureRecognizerState.Changed :

            self.updateInteractiveTransition(d)

        default :

            interactive = false

            if d > 0.2 || velocity.x > 0 {
                self.finishInteractiveTransition()
            }
            else {
                self.cancelInteractiveTransition()
            }
        }

    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

        //Something....


        UIView.animateWithDuration(1.0, animations: {

            //Something....

            }, completion: {}

        })

    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 1
    }

    // MARK: UIViewControllerTransitioningDelegate protocol methods

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.presenting = true
        return self
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.presenting = false
        return self
    }

    func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return self.interactive ? self : nil
    }

    func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return self.interactive ? self : nil
    }





}

事实上,我有很多与此相似的代码,这个代码正在运行,但其他代码非常相似,正是我所解释的:segue只是在没有交互式转换的情况下执行。有人可以解释一下吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

如果问题是您的互动过渡动画没有互动,我建议在interactionControllerForPresentation中设置一个断点:

  • 确保调用interactionControllerForPresentation的正确实例,就像您认为的那样。

    例如,请确保viewDidLoad目的地没有替换您在源代码管理器中transitioningDelegate中仔细设置的prepareForSegue ...这很容易如果从A到B进行交互式过渡,然后再从B到C进行交互过渡,那么预期B到C的设置可能会干扰你打算从A到B做什么。

  • 确保interactionControllerForPresentation返回您认为应该的值(即确保interactive绝对是true)。

每当我看到手势的这种行为没有以交互方式启动过渡时,就像我认为的那样,那是因为在目标控制器interactionControllerForPresentation被调用时transitioningDelegate UIViewControllerInteractiveTransitioning 1}},它没有像我预期的那样返回有效的UIPercentDrivenInteractiveTransition(例如interactionControllerForPresentation)。

无论如何,您可以通过在self.interactive中添加日志语句来诊断这一点,并且您会看到(a)由于某种原因未被调用;或(b)true不是{{1}}。