理论问题:在下面的代码中,为什么在"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
}
情节提要
分段是从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只是在没有交互转换的情况下执行的。有人能给我解释一下吗?
感谢您的帮助
如果问题是交互式转换动画不是交互式的,我建议在interactionControllerForPresentation
:中设置断点
-
确保
interactionControllerForPresentation
的正确实例按照您认为的方式调用。例如,确保destination的
viewDidLoad
没有替换您在源控制器的prepareForSegue
中精心设置的transitioningDelegate
。。。如果从A到B,再从B到C进行交互式转换,那么这很容易做到,你在预期B到C时设置的内容可能会干扰你从A到C的意图。 -
确保
interactionControllerForPresentation
返回您认为应该返回的值(即,确保interactive
肯定是true
)。
每次我看到手势没有以交互方式启动转换的行为,就像我认为应该的那样,都是因为当在目标控制器的transitioningDelegate
中调用interactionControllerForPresentation
时,它没有像我预期的那样返回有效的UIViewControllerInteractiveTransitioning
(如UIPercentDrivenInteractiveTransition
)。
无论如何,您可以通过在interactionControllerForPresentation
中添加一条日志语句来诊断这一问题,并且您会看到(a)由于某种原因没有调用它;或者(b)CCD_ 14不是CCD_。