The old way of adding a gesture recognizer no longer works because the view is always nil. This is because there is no view actively presenting the SpriteView scene in SwiftUI.
class GameScene: SKScene {
override func sceneDidLoad() {
let gestureSwipe = UISwipeGestureRecognizer()
gestureSwipe.addTarget(self, action: #selector(gestureActionHandleSwipe(_:)))
view?.addGestureRecognizer(gestureSwipe)
/// `view` is always nil
struct ContentView: View {
var body: some View {
SpriteView(scene: GameScene())
}
To present a scene, you call the presentScene(:) method or presentScene(:transition:) method on the SKView class. If the scene is not currently presented, this property holds nil. https://developer.apple.com/documentation/spritekit/skscene/1519726-view
Simulating a drag gesture with the touchesMoved function seems easy. However, simulating a gesture like pinch is not as straightforward. So how can we gesture recognizer for a SpriteKit scene in SwiftUI?