从 UIViewControllerRepresentable 向 swiftui 视图发送一个事件

问题描述

我尝试在 SwiftUI 视图中捕获发生在 UIViewController 中的事件。 架构在 SwiftUI 中,在 Swift 中有一个元素:

视图

struct PlayGame: View {
    var body: some View {
          if stateProgress.stateOfGame == 0 {
            CardsDeckRepresentable()
        }
    }
}

UIViewControllerRepresentable

struct CardsDeckRepresentable: UIViewControllerRepresentable {
    typealias UIViewControllerType = CardsDeckViewController
    

    func makeUIViewController(context: Context) -> CardsDeckViewController {
        let deck = CardsDeckViewController()
        return deck
    }
    
    func updateUIViewController(_ uiViewController: CardsDeckViewController,context: Context) {

    }
}

UIViewController

class CardsDeckViewController : UIViewController,iCarouselDelegate,iCarouselDataSource{

... some code ...

    func moveCardChoosen(number:Int) {
        
        self.view.addSubview(cardMobile)

        UIView.animate(withDuration: 0.5,delay: 0.0,options:[],animations: {
            self.cardMobile.center.y = self.cardplace5.center.y
        },completion: { finished in
            if finished {
                self.cardMobile.isHidden = true
            }
        })
    }
}

在动画结束时,我想通知 swiftUIView 进行更新。

我尝试使用一些 ObservableObject 或使用 updateUIViewController 函数。 我可以通过 UIViewControllerRepresentable 将数据从 SwiftUI 视图传递到 UIViewController。 但是如何从 UIViewController 恢复更改?似乎没有调用 updateUIViewController。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)