Swift自定义转换不起作用(未调用UIViewControllerTransitioningDelegate)



我只是想切换到第二个视图控制器。我有我的HomeViewController与这个扩展:

extension HomeViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController)
-> UIViewControllerAnimatedTransitioning? {
return transition
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
}

和类中定义的转换

let transition = PopAnimator()

我有一个按钮,当它被点击时,应该切换视图控制器并使用自定义转换(PopAnimator(:

@objc func handleTap() {
let viewController = ViewController()
viewController.transitioningDelegate = self
self.present(viewController, animated: false, completion: nil)
}

PopAnimator类(现在实际上只是一个fadeIn(:

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let duration = 1.0
var presenting = true
var originFrame = CGRect.zero
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toView = transitionContext.view(forKey: .to)!
containerView.addSubview(toView)
toView.alpha = 0.0
UIView.animate(withDuration: duration,
animations: {
toView.alpha = 1.0
},
completion: { _ in
transitionContext.completeTransition(true)
}
)
}
}

当我单击按钮时,它会切换视图控制器,但不使用我的自定义转换。如果我在animationController(forPresented:presenting:source:(函数或PopAnimator类中放置断点,则无法到达。

self.present(viewController, animated: false, completion: nil)

如果您想要动画转换,请尝试设置animated: true

最新更新