使用 UIKit 和 SwiftUI 实现 UIAnimatedNavigationTransition

问题描述 投票:0回答:1

当使用 UIKit 在 iOS 应用程序中创建动画过渡时,我们通常在两个视图控制器之间使用 UIAnimatedNavigationTransition。然而,一项新的业务需求需要将其中一个视图控制器替换为 SwiftUI 视图。我们如何使动画导航过渡在 SwiftUI 视图和 UIKit 视图控制器之间工作?有没有办法实现这个目标?

此外,是否有任何文档解释了 UIHostingController 是如何创建的,而不仅仅是如何实现它?了解它的创建可能有助于设计一个解决方法,但我还没有找到任何关于此的资源。

ios swift swiftui uikit
1个回答
0
投票

您需要创建一个动画师和过渡委托

    import UIKit

    class Animator: NSObject, UIViewControllerAnimatedTransitioning {

       func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
       }

       func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let fromVC = transitionContext.viewController(forKey: .from),
              let toVC = transitionContext.viewController(forKey: .to) else {
            return
        }

        let containerView = transitionContext.containerView

        containerView.addSubview(toVC.view)
        toVC.view.frame = fromVC.view.frame
        toVC.view.alpha = 0.0

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            toVC.view.alpha = 1.0
        }) { finished in
            fromVC.view.removeFromSuperview()
            transitionContext.completeTransition(finished)
        }
       }
}

import UIKit

class TransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return Animator()
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return Animator()
    }
}

然后将转换委托应用于按钮操作上的视图控制器文件中托管视图控制器的用户

//MARK: - Properties
let transitionDelegate = TransitioningDelegate()

//MARK: - Button Action
@objc func showSwiftUIView() {
    let swiftUIView = SwiftUIView()
    let hostingController = UIHostingController(rootView: swiftUIView)
    hostingController.modalPresentationStyle = .fullScreen
    hostingController.transitioningDelegate = transitionDelegate
    present(hostingController, animated: true, completion: nil)
}
© www.soinside.com 2019 - 2024. All rights reserved.