Fayyouz
Fayyouz

Reputation: 682

How can SKScene communicate with its container SKView?

I have a SKScene presented in a SKView. I want a way to send info from the scene to the view, for later use with the controller.

For example, consider having the View Controller as the delegate of the SKView, and the SKView needs to notify the UIViewController when a certain part of its presented scene is tapped.

Upvotes: 2

Views: 107

Answers (1)

Robert
Robert

Reputation: 6810

SKScene has a handy instance member .view for just this situation. To access standard SKView methods and properties you can use it like:

self.view?.addGestureRecognizer(pinchGestureRecognizer)

If your containing view is an SKView subclass, you can downcast .view to access custom methods:

if let myView = self.view as? MyCustomViewClass {
    myView.sceneWasTapped()
}

Keep in mind that .view is an optional; it will be nil if the scene hasn't been added to a view yet. Scenes call didMove(to view: SKView) when they are added to a view, so that's a good place to do things that rely on a .view existing.

Upvotes: 3

Related Questions