Interactive transition with no button press Segue (Swift)

I got lost in the universe of transitions. I want an interactive transition using push segue. The following code works with a modal segment, but not with a single click:

(Using the push segment, the animation is not interactive and is canceled)

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 } 

TransitionManager.swift

 class TransitionManager: UIPercentDrivenInteractiveTransition,UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate,UIViewControllerInteractiveTransitioning { var interactive = false var presenting = false var panGesture : UIPanGestureRecognizer! var destViewController : UIViewController! 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 velocity = pan.velocityInView(pan.view!) let d = translation.x / pan.view!.bounds.width * 0.5 switch pan.state { case UIGestureRecognizerState.Began : interactive = true sourceViewController.performSegueWithIdentifier("1to2", 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) { // create a tuple of our screens let screens : (from:UIViewController, to:UIViewController) = (transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!, transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!) let container = transitionContext.containerView() let toView = screens.to.view let fromView = screens.from.view toView.frame = CGRectMake(-320, 0, container.frame.size.width, container.frame.size.height) container.addSubview(toView) container.addSubview(fromView) let duration = self.transitionDuration(transitionContext) // perform the animation! UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: nil, animations: { toView.frame.origin = container.frame.origin fromView.frame.origin = CGPointMake(320, 0) }, completion: { finished in if(transitionContext.transitionWasCancelled()){ transitionContext.completeTransition(false) UIApplication.sharedApplication().keyWindow.addSubview(screens.from.view) } else { transitionContext.completeTransition(true) UIApplication.sharedApplication().keyWindow.addSubview(screens.to.view) } }) } 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 } 

}

Storyboard

Segu from FirstViewController to SecondViewController.

  • ID: "1to2"

  • Segue: click

  • Appointment: current

thanks for your help

+5
source share

Source: https://habr.com/ru/post/1205540/


All Articles