推送诡计不起作用的交互式过渡(斯威夫特)

时间:2014-10-26 13:03:12

标签: ios swift push transition interactive

我迷失在过渡的世界里。我想要一个推送segue的交互式过渡。以下代码适用于模态segue,但不适用于push one:

(使用push segue,动画不是交互式的并且是相反的)

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
}

}

故事板

segue是从FirstViewController到SecondViewController。

  • 标识符:“1to2”

  • Segue:推

  • 目的地:当前

感谢您的帮助

0 个答案:

没有答案