UIView自定义转换在完成时快速恢复

时间:2016-06-22 10:02:45

标签: swift animation uiview transition caanimation

我已经实现了一个类BubbleAnimator,它应该在视图之间创建一个类似于气泡的过渡,并通过UIViewControllerTransitioningDelegate - 协议添加它。到目前为止,呈现动画工作正常(这就是为什么我没有添加此部分的所有代码)。

但是在解除视图时,动画结束时'fromViewController'会闪烁。在这个非常短的闪光之后,再次显示正确的toViewController,但是这个故障非常烦人。 以下是相关的animateTransition - 方法:

    //Get all the necessary views from the context
    let containerView = transitionContext.containerView()
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)

    //Presenting
    if self.reverse == false {

        //Add the destinationvc as subview
        containerView!.addSubview(fromViewController!.view)
        containerView!.addSubview(toViewController!.view)

        /*...Animating the layer goes here... */

    //Dismissing
    } else {
        containerView!.addSubview(toViewController!.view)
        containerView!.addSubview(fromViewController!.view)

        //Init the paths
        let circleMaskPathInitial = UIBezierPath(ovalInRect: self.originFrame)
        let extremePoint = CGPoint(x: originFrame.origin.x , y: originFrame.origin.y - CGRectGetHeight(toViewController!.view.bounds) )
        let radius = sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y))
        let circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(originFrame, -radius, -radius))

        //Create a layer
        let maskLayer = CAShapeLayer()
        maskLayer.path = circleMaskPathFinal.CGPath
        fromViewController!.view.layer.mask = maskLayer


        //Create and add the animation
        let animation = CABasicAnimation(keyPath: "path")
        animation.toValue = circleMaskPathInitial.CGPath
        animation.fromValue = circleMaskPathFinal.CGPath
        animation.duration = self.transitionDuration(transitionContext)
        animation.delegate = self
        maskLayer.addAnimation(animation, forKey: "path")
    }

清理发生在委托方法中:

override public func animationDidStop(anim: CAAnimation, finished flag: Bool) {
    self.transitionContext?.completeTransition(!(self.transitionContext?.transitionWasCancelled())!)
}

我想,我在将视图添加到containerView时做错了,但我无法理解。另一种可能性是,当调用函数completeTransition时,视图的图层蒙版会被重置。

1 个答案:

答案 0 :(得分:2)

感谢this blogpost我终于能够解决这个问题了。简短说明:

CAAnimation仅操纵视图的表示层,但不会更改模型层。当动画现在完成时,它的值会快速恢复到模型层的原始值和未更改的值。

简短而简单的解决方法:

        animation.fillMode = kCAFillModeForwards
        animation.removedOnCompletion = false

更好的解决方案,因为它不会阻止动画被删除是在动画开始之前手动设置图层位置的最终值。这样,为模型层分配了正确的值:

        maskLayer.path = circleMaskPathInitial.CGPath
        //Create and add the animation below..