Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

watchOS 3.0 detect crown rotation in SpriteKit

So as of watchOS 3.0 you are now able to get the rotation of the digital crown. I managed to use the crownDidRotate function in an InterfaceController. But I can't get the rotation of the crown from inside a SKScene Class. Can anybody help me with this I'm pretty lost right now? Thanks.

like image 438
cronenberg Avatar asked Feb 06 '23 19:02

cronenberg


1 Answers

To get those crownDidRotate calls in your interface controller, you had to adopt the WKCrownDelegate protocol in your interface controller, and set your interface controller as the delegate of its crownSequencer.

To get crownDidRotate calls in some other class, adopt the WKCrownDelegate protocol in that class, and set an instance of that class as the delegate of your interface controller's crownSequencer.

Presumably you already have some code like this to set up your SpriteKit scene:

class InterfaceController: WKInterfaceController {

    @IBOutlet var spriteGizmo: WKInterfaceSKScene!

    override func awake(withContext context: AnyObject?) {
        super.awake(withContext: context)

        let scene = MyScene(fileNamed: "MyScene")
        spriteGizmo.presentScene(MyScene(fileNamed: "MyScene"))
    }
}

If you've declared WKCrownDelegate conformance in your MyScene class, just add a line to set it as the delegate of the interface controller's crown sequencer:

let scene = MyScene(fileNamed: "MyScene")
spriteGizmo.presentScene(MyScene(fileNamed: "MyScene"))
crownSequencer.delegate = scene

(Alternatively, you may set your WKInterfaceSKScene's scene in the Storyboard. In that case, you can still reference the WKInterfaceSKScene from your interface controller with an IBOutlet. Then in awake(withContext:), you can access the scene through that outlet and set it as the crown delegate.)

like image 110
rickster Avatar answered Feb 21 '23 04:02

rickster