Search code examples
iosswiftuiviewsprite-kitcore-motion

Is it possible to use a SKScene with a UIView Controller?


I am using CoreMotion in my view to get the acceleration and rotation values in my phone. My app has multiple views that uses a view controller because I deal with data transfer, so I set up my view of the accelerometer/gyroscope with UIViewController. However I want to show a image of a ball that moves across the screen based on the values I get off the accelerometer and the gyroscope. I have been looking across at tutorials and that seems to be only done with games. Is there a way I can show a ball that uses CoreMotion to move without using SKScene?


Solution

  • Is it possible to use a SKScene with a UIView Controller?

    Yes. You're probably doing it already without realizing it.

    The bridge between SpriteKit and Cocoa Touch's view architecture is SKView. A lot of SpriteKit-based apps just have one view controller whose view is a SKView, and then they handle different phases of the game by doing all their drawing in SpriteKit and just changing sprite scenes. But you don't have to work that way -- you can mix SKView with other views and controls, and you can use as many view controllers as you like.

    Here's a shot of a SKView inside another view and managed by a view controller that's embedded in a navigation controller:

    Hello, world!

    The purple area is a plain old UIView -- only the black area is a SpriteKit view.

    Is there a way I can show a ball that uses CoreMotion to move without using SKScene?

    Yes. If all you want to do is to move a ball across the screen, the UIKit animation built into UIView is almost certainly more than enough.