动画过渡

时间:2017-11-26 13:59:12

标签: ios animation uikit

我的下面有我的功能,可以设置从单元格项到视图控制器的转换动画。似乎toViewController子视图在动画开始时将具有其全宽(附加的gif上的蓝色部分和工具栏)。对它为什么会发生的任何想法?

enter image description here

class CardPresentationAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    var originFrame: CGRect

    init(originFrame: CGRect) {
        self.originFrame = originFrame
        super.init()
    }

    var duration: TimeInterval = 0.7

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

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let toViewController = transitionContext.viewController(forKey: .to) else {
            return
        }   

        let containerView = transitionContext.containerView
        let finalFrame = transitionContext.finalFrame(for: toViewController)

        let animationDuration = transitionDuration(using: transitionContext)

        // Set the presented view controller the frame same as the origin frame
        containerView.addSubview(toViewController.view)
        toViewController.view.frame = originFrame

        UIView.animate(withDuration: animationDuration, animations: { 
             toViewController.view.frame = finalFrame
        }) { (finished) in
            transitionContext.completeTransition(finished)
        }
    }
}

0 个答案:

没有答案