我正在尝试为新的视图控制器实现自定义动画。第一部动画效果很好,但第二部动画并不顺畅。我在过去2个小时内玩过代码,但仍无法弄清楚。该类使用UIViewControllerAnimatedTransitioning协议。
这是一段视频,向您展示它的外观 https://www.youtube.com/watch?v=kEDXpk3HMe0
你会在最后看到它只是跳到屏幕的整个尺寸。
扩展名CustomPresentedViewController:UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomModalShowAnimator()
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
}
class CustomModalShowAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 2
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) else { return }
let transitionContainer = transitionContext.containerView
var transform = CGAffineTransform.identity
transform = transform.concatenating(CGAffineTransform(scaleX: 45, y: 0))
toViewController.view.transform = transform
toViewController.view.alpha = 0
transitionContainer.addSubview(toViewController.view)
let animationTiming = UISpringTimingParameters(dampingRatio: 1, initialVelocity: CGVector(dx: 5, dy: 0))
let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), timingParameters: animationTiming)
animator.addAnimations {
toViewController.view.transform = CGAffineTransform.identity
toViewController.view.alpha = 1
}
animator.addCompletion { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
animator.startAnimation()
}
}