cronenberg
cronenberg

Reputation: 97

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.

Upvotes: 4

Views: 1063

Answers (2)

E. Rivera
E. Rivera

Reputation: 10938

In watchOS 3 just any object object can get digital crown events by setting them as a delegate:

let crownSequencer = WKExtension.shared().rootInterfaceController!.crownSequencer
crownSequencer.delegate = self
crownSequencer.focus()

Then read back the value by implementing:

func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double)

It is important to call the focus(), especially for controllers whose UI fits the screen and do not need actual scrolling.

Upvotes: 0

rickster
rickster

Reputation: 126127

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.)

Upvotes: 8

Related Questions