0

I try to catch an event that occurs in a UIViewController in the SwiftUI view. the architecture is in SwiftUI with an element in Swift:

The View

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

The UIViewControllerRepresentable

struct CardsDeckRepresentable: UIViewControllerRepresentable {
    typealias UIViewControllerType = CardsDeckViewController
    

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

    }
}

The 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
            }
        })
    }
}

At the end of the animation, I want to tel the swiftUIView to do update.

I tried with some ObservableObject or using the updateUIViewController function. I can pass data from the SwiftUI View to the UIViewController through the UIViewControllerRepresentable. But how to revive the change from UIViewController? The updateUIViewController don't seems not be called.

Rufilix
  • 253
  • 3
  • 22
  • 1
    Would you explain witch event do you want to send to, I think, PlayGame and what should happen on it? – Asperi Apr 07 '21 at 12:06
  • Thanks @Asperi. I want notify PlayGame that the animation in CardsDeckViewController finished. Then It should call a function or update a variable in PlayGame – Rufilix Apr 07 '21 at 15:46
  • 1
    Does this answer your question https://stackoverflow.com/a/65547006/12299030? – Asperi Apr 07 '21 at 15:56
  • It's interesting. But the action come from the SwiftUI View. I need to call the action in the Controller. But will take a closer look at this code. – Rufilix Apr 07 '21 at 16:33
  • in fact, I'm looking of the opposite of that question: https://stackoverflow.com/questions/59143053/how-to-execute-a-function-in-a-uiviewcontroller-through-a-swiftui-button – Rufilix Apr 07 '21 at 17:03

0 Answers0