自定义动画过渡 - 查看/查看

时间:2015-01-24 17:30:28

标签: ios swift uiview transition uiviewanimation

我有一个用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)

    })
}

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

任何关于如何达到预期效果的建议都将受到赞赏。

有缺陷的代码:

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)

    })
}

1 个答案:

答案 0 :(得分:3)

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

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