用动画打开视图控制器

时间:2016-08-30 05:48:57

标签: ios swift custom-transition

我想打开一个带有动画的视图控制器,右下方有一个方形按钮的框架:

enter image description here

我想移动视图控制器的顶部和底部。

我试图实现这一点,但这不是那么好,我不知道如何隔离不同的边缘:

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    let duration    = 0.6
    var presenting  = true
    var originFrame = CGRect.zero

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

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

        let containerView = transitionContext.containerView()!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        let herbView = presenting ? toView : transitionContext.viewForKey(UITransitionContextFromViewKey)!

        let initialFrame = presenting ? originFrame : herbView.frame
        let finalFrame = presenting ? herbView.frame : originFrame

        let xScaleFactor = presenting ?
            initialFrame.width / finalFrame.width :
            finalFrame.width / initialFrame.width

        let yScaleFactor = presenting ?
            initialFrame.height / finalFrame.height :
            finalFrame.height / initialFrame.height

        let scaleTransform = CGAffineTransformMakeScale(xScaleFactor, yScaleFactor)

        if presenting {
            herbView.transform = scaleTransform
            herbView.center = CGPoint(
                x: CGRectGetMidX(initialFrame),
                y: CGRectGetMidY(initialFrame))
            herbView.clipsToBounds = true
        }

        containerView.addSubview(toView)
        containerView.bringSubviewToFront(herbView)

        UIView.animateWithDuration(duration, delay:0.0,
                                   options: [],
                                   animations: {
                                    herbView.transform = self.presenting ?
                                        CGAffineTransformIdentity : scaleTransform

                                    herbView.center = CGPoint(x: CGRectGetMidX(finalFrame),
                                        y: CGRectGetMidY(finalFrame))

            }, completion:{_ in
                transitionContext.completeTransition(true)
        })

        let round = CABasicAnimation(keyPath: "cornerRadius")
        round.fromValue = presenting ? 3.0/xScaleFactor : 0.0
        round.toValue = presenting ? 0.0 : 3.0/xScaleFactor
        round.duration = duration / 2
        herbView.layer.addAnimation(round, forKey: nil)
        herbView.layer.cornerRadius = presenting ? 0.0 : 3.0/xScaleFactor
    }

}

你知道我怎么做吗?

0 个答案:

没有答案