mm24
mm24

Reputation: 9606

Using watch crown to control a WKInterfaceSlider

I would like to use the apple watch crown to control a slider. Is this possible?

If so, how?

Apple uses it to change the colours on the UI of the watch.

EDIT: so it seems not possible at the moment (see answers below). Is important to notice that in two weeks time (Apple WWDC 2015) this could change (maybe a Watch OS for independent apps?)

Upvotes: 11

Views: 2790

Answers (5)

kelin
kelin

Reputation: 12021

All answers are outdated.

In watchOS 3 Apple introduced the WKCrownSequencer class, that allows to track the user's interaction with the digital crown. You should implement the WKCrownDelegate to be notified about rotation changes, then map rotation angle to the change of the value. Here is an example of how to control a WKInterfaceSlider with help of the crown:

class SliderInterfaceController: WKInterfaceController {
    @IBOutlet var slider: WKInterfaceSlider!

    let minVal: Float = 0
    let maxVal: Float = 10

    var selectedVal: Float = 0

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

        // The sequencer should be focused to receive events
        crownSequencer.focus()
        crownSequencer.delegate = self
    }

    @IBAction func sliderAction(_ value: Float) {
        selectedVal = value
    }
}

// MARK: WKCrownDelegate
extension SliderInterfaceController: WKCrownDelegate {
    func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
        // 1 divided by number of rotations required to change the value from min to max 
        let rotationToValRatio = 0.25 * Double(maxVal - minVal)

        let newVal = selectedVal + rotationalDelta * rotationToValRatio

        let trimmedNewVal = max(minVal, min(newVal, maxVal))

        slider.setValue(Float(trimmedNewVal))
        selectedVal = trimmedNewVal
    }
}

Upvotes: 6

Thilo
Thilo

Reputation: 989

Since watchOS 2 beta you can use the digital crown with WKInterfacePicker!

In order to use the digital crown in combination with an WKInterfaceSlider you need to do a little workaround:

  • add a WKInterfacePicker to your Interface Controller and set the height of it to 0 in order to hide it from the user (it won't be accessible if you set 'hidden' to true)
  • generate an array with the number of steps of your slider, example:

    items = [WKPickerItem](count: numberOfSteps, repeatedValue: WKPickerItem())
    picker.setItems(items)
  • call picker.focus() in order to receive digital crown input

  • add an action to your picker that sets the slider value, example:

    @IBAction func pickerChanged(value: Int) {
        slider.setValue(Float(value + 1))
    }

Upvotes: 22

Pavel Smejkal
Pavel Smejkal

Reputation: 3599

Not possible. You don't have the crown in control – it's used only automatically for scrolling etc..

Apple guidelines:

"Scrolling is the only supported Digital Crown interaction for apps, and the system automatically manages those interactions for you. Apps do not have direct access to the Digital Crown."

https://developer.apple.com/watch/human-interface-guidelines/

Apple might enable digital crown when the Apple Watch native apps are out (we will see in 2 weeks at WWDC).

Upvotes: 2

John
John

Reputation: 8548

Currently, this is not possible.

However, as has been announced on May 27th, Apple will introduce an SDK for native watch apps at WWDC in June. This will allow access to the digital crown.

Source: http://9to5mac.com/2015/05/27/live-blog-apple-senior-vp-of-operations-jeff-williams-interview-at-code-conference/

Upvotes: 0

Alexander Gattringer
Alexander Gattringer

Reputation: 175

Like ps4 said, this is simply not possible.

As Apple states in Watch design guidelines:

"Scrolling is the only supported Digital Crown interaction for apps, and the system automatically manages those interactions for you. Apps do not have direct access to the Digital Crown."

https://developer.apple.com/watch/human-interface-guidelines/

Upvotes: 0

Related Questions