Interactive Navigation: Segue Declaration
Theoretical question: in the code below, why, on the line "performSegue ....", the segue is not being executed completely? I mean, how does he know this is an interactive transition, and why does the "performSegue ..." method not have the same effect as if it were declared somewhere else?
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
}
Storyboard
Shogu from FirstViewController to SecondViewController.
-
Identifier: "segueIdentifier"
-
Segue: Modal
-
Purpose: current
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
}
}
In fact, I have a lot of code similar to this, this one works, but others do very similarly what I am explaining: the segue is simply executed without an interactive transition. Can someone explain to me?
thanks for the help
source to share
If the problem is that your interactive transition animation is not interactive, I would suggest putting a breakpoint at interactionControllerForPresentation
:
-
Make sure the correct instance
interactionControllerForPresentation
is being called as you think.For example, make sure
viewDidLoad
not to replace the targettransitioningDelegate
that you set so carefullyprepareForSegue
in the original controller ... this is easy to do if you interactively jump from A to B and then B to C again, which is what you configured in anticipation of B to C might interfere with what you intended to do A to B. -
Make sure it
interactionControllerForPresentation
returns the value you think it should (i.e. make sure itinteractive
definitely doestrue
).
Every time I saw this gesture behavior not triggering an interactive transition, I think it should have happened because when interactionControllerForPresentation
called in the destination controller transitioningDelegate
, it did not return valid UIViewControllerInteractiveTransitioning
(for example UIPercentDrivenInteractiveTransition
) as I expected.
Either way, you can diagnose this by adding a log statement to interactionControllerForPresentation
, and you will either see that (a) it is not called for any reason; or (b) self.interactive
not true
.
source to share