自定义动画过渡 - 查看 in/view 出来

Custom animated transition - view in/view out

我有一个用 Swift 编写的自定义转换,当呈现的视图从侧面进入时,被关闭的视图消失。

现在我想要同样的效果,但我希望呈现的视图从顶部进入,而关闭的视图从底部出来。

我的代码如下所示:

func animateTransition(transitionContext: UIViewControllerContextTransitioning){

    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

    let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
    let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)


    if self.presenting == true{
        toView?.transform = offScreenLeft
    }else{
        toView?.transform = offScreenRight
    }

    container.addSubview(toView!)
    container.addSubview(fromView!)

    let duration = self.transitionDuration(transitionContext)

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.8, options: nil, animations: {

        if self.presenting == true{
            fromView?.transform = offScreenRight
        }else{
            fromView?.transform = offScreenLeft
        }
        toView?.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })
}

我认为这很容易,因为我的逻辑告诉我们这样做的方法是在 toView 和 fromView 中从 "width" 更改为 "height",但这不起作用,并且只是创建了与以前相同的效果,但它似乎跳过了一个空(黑色)视图。

如有任何关于如何达到预期效果的建议,我们将不胜感激。

错误代码:

func animateTransition(transitionContext: UIViewControllerContextTransitioning){

    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

    let offScreenUp = CGAffineTransformMakeTranslation(container.frame.height, 0)
    let offScreenDown = CGAffineTransformMakeTranslation(-container.frame.height, 0)


    if self.presenting == true{
        toView?.transform = offScreenDown
    }else{
        toView?.transform = offScreenUp
    }

    container.addSubview(toView!)
    container.addSubview(fromView!)

    let duration = self.transitionDuration(transitionContext)

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.8, options: nil, animations: {

        if self.presenting == true{
            fromView?.transform = offScreenUp
        }else{
            fromView?.transform = offScreenDown
        }
        toView?.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })
}

您仍在尝试在 X 坐标中进行平移。尝试在 Y 坐标中进行平移。

let offScreenUp = CGAffineTransformMakeTranslation(0,container.frame.height)
let offScreenDown = CGAffineTransformMakeTranslation(0,-container.frame.height)