自定义ViewController转换

时间:2015-11-18 05:49:24

标签: ios swift uiviewcontroller

我在Appcoda Transition ViewControllers transition上看到了一个从上到下的菜单,我实现了它。然后,我尝试使用transition自下而上UIViewControllerContextTransitioning。但是,做错了因为我认为我设置了错误的价值观。以下是代码

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

    //Get reference to our fromView, toView and the container view
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

    //Setup the transform for sliding
    let container = transitionContext.containerView()
    let height = container?.frame.height
    let moveDown = CGAffineTransformMakeTranslation(0, height! - 150)
    let moveUp = CGAffineTransformMakeTranslation(0, -50)

    //Add both views to the container view
    if isPresenting {
        toView?.transform = moveUp
        snapShot = fromView?.snapshotViewAfterScreenUpdates(true)
        container?.addSubview(toView!)
        container?.addSubview(snapShot!)
    }

    //Perform the animation
    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.3, options: UIViewAnimationOptions(rawValue: 0), animations: {
        if self.isPresenting {
            self.snapShot?.transform = moveDown
            toView?.transform = CGAffineTransformIdentity
        } else {
            self.snapShot?.transform = CGAffineTransformIdentity
            fromView?.transform = moveUp
        }
        }, completion: {finished in

            transitionContext.completeTransition(true)
            if !self.isPresenting {
                self.snapShot?.removeFromSuperview()
            }
    })
}

1 个答案:

答案 0 :(得分:1)

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

func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
    // Get reference to our fromView, toView and the container view
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

    // Set up the transform we'll use in the animation
    guard let container = transitionContext.containerView() else {
        return
    }

    let moveUp = CGAffineTransformMakeTranslation(0, container.frame.height + 50)
    let moveDown = CGAffineTransformMakeTranslation(0, -250)


    // Add both views to the container view
    if isPresenting {
        toView.transform = moveUp
        snapshot = fromView.snapshotViewAfterScreenUpdates(true)
        container.addSubview(toView)
        container.addSubview(snapshot!)
    }

    // Perform the animation
    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: {

        if self.isPresenting {
            self.snapshot?.transform = moveDown
            toView.transform = CGAffineTransformIdentity
        } else {
            self.snapshot?.transform = CGAffineTransformIdentity
            fromView.transform = moveUp
        }

        }, completion: { finished in

            transitionContext.completeTransition(true)

            if !self.isPresenting {
                self.snapshot?.removeFromSuperview()
            }
    })
}

这应该有效。我查看了你分享的教程,你可能看不到底部的菜单,因为在故事板上设置了MenuTableViewController.swift的方式,所以菜单总是从顶部开始,所以改变它并且它应该工作得很好。如果您有任何疑问,请告诉我。