要解决不要推根视图控制器的从右到左过渡的问题,可以使用以下方法:
class CustomTransition: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromViewController = transitionContext.viewController(forKey: .from),
let toViewController = transitionContext.viewController(forKey: .to) else {
transitionContext.completeTransition(false)
return
}
let containerView = transitionContext.containerView
containerView.addSubview(toViewController.view)
toViewController.view.frame = containerView.bounds
UIView.transition(from: fromViewController.view,
to: toViewController.view,
duration: transitionDuration(using: transitionContext),
options: .transitionCrossDissolve) { _ in
transitionContext.completeTransition(true)
}
}
}
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
transitioningDelegate = self
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return CustomTransition()
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
}
let viewController = SecondViewController()
viewController.modalPresentationStyle = .fullScreen
viewController.transitioningDelegate = self
present(viewController, animated: true, completion: nil)
通过以上方法,可以实现不推根视图控制器的从右到左过渡的解决方案。