Member-only story
7 Ways to Link SwiftUI Views to SpriteKit Scene
A look at some different ways to link a class with a SwiftUI interface
I was playing around with SpriteKit, and it occurred to me that it might be a good exercise to remind myself of how many different ways I could link my SwiftUI code with my SpriteKit scene/class. I don’t present these in any order, preference, or recommendation; I show them in the order that they came into my head more or less.
The Brief
I am running a SpriteKit scene within a SwiftUI app, and I want to be able to control a sprite directly using a SwiftUI element, a button in this case. Join me to see how many ways I can cook up — to do just that.
Let’s begin with the basic code. Now I present both structs/classes in the same file, although you will want to have them defined in separate ones really.
With the end goal to come up with as many different ways to do this as I can, the blue text jump
is a SwiftUI button. The rabbit is a SKSpriteNode
inside a scene hosted by my SwiftUI…