iOS UINavigationController自定义InteractionController在AnimationController中使用Core Animation时无法正常工作

时间:2017-11-24 08:59:50

标签: ios animation uinavigationcontroller core-animation custom-transition

我有自定义动画控制器,我用于导航控制器的转换,我想让它交互,所以我创建了一个自定义的交互控制器,它适用于其中一些,但在我使用CoreAnimations的那些执行它无法正常工作的动画。

它自己启动并完成动画,因为在未调用交互控制器finish()时,我留下了黑屏。

这是AnimationController中的代码:

    CATransaction.begin()
    let revealAnimation = CABasicAnimation(keyPath: "path")
    revealAnimation.toValue = UIBezierPath(arcCenter: originPoint, radius: UIScreen.main.bounds.size.height*1.5, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true).cgPath
    revealAnimation.duration = transitionDuration
    revealAnimation.isRemovedOnCompletion = false
    revealAnimation.fillMode = kCAFillModeForwards
    revealAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    circleMask.add(revealAnimation, forKey: nil)

    let fadeAnimation2 = CABasicAnimation(keyPath: "opacity")
    fadeAnimation2.toValue = 1.0
    fadeAnimation2.duration = 0
    fadeAnimation2.beginTime = CACurrentMediaTime() + transitionDuration/2
    fadeAnimation2.isRemovedOnCompletion = false
    fadeAnimation2.fillMode = kCAFillModeForwards
    fadeAnimation2.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    toViewController.view.layer.add(fadeAnimation2, forKey: nil)

    let fadeAnimation = CABasicAnimation(keyPath: "fillColor")
    fadeAnimation.toValue = UIColor.clear.cgColor
    fadeAnimation.duration = transitionDuration/4
    fadeAnimation.beginTime = CACurrentMediaTime() + 3*transitionDuration/4
    fadeAnimation.isRemovedOnCompletion = false
    fadeAnimation.fillMode = kCAFillModeForwards
    fadeAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    fadeAnimation.delegate = self
    circleMask.add(fadeAnimation, forKey: nil)

    CATransaction.commit()

在我的CAAnimationDelegate中:

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    if flag {
        circleMask.removeAllAnimations()
        circleMask.removeFromSuperlayer()
        toViewController.view.layer.opacity = 1
        toViewController.view.layer.removeAllAnimations()
        if transitionContext.transitionWasCancelled {
            toViewController.view.removeFromSuperview()
        } else {
            fromViewController.view.removeFromSuperview()
        }
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    }

}

最后这是InteractionController:

@objc func handleGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
    let translation = gestureRecognizer.translation(in: gestureRecognizer.view!.superview!)
    var progress = (translation.x / 200)
    progress = CGFloat(fminf(fmaxf(Float(progress), 0.0), 1.0))

    switch gestureRecognizer.state {
    case .began:
        interactionInProgress = true
        viewController?.navigationController?.popViewController(animated: true)
    case .changed:
        shouldCompleteTransition = progress > 0.5
        update(progress)
    case .cancelled:
        interactionInProgress = false
        cancel()
    case .ended:
        interactionInProgress = false
        if shouldCompleteTransition {
            finish()
        } else {
            cancel()
        }
    default:
        break
    }
}

我做错了什么?

0 个答案:

没有答案