Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using watch crown to control a WKInterfaceSlider

Tags:

ios

watchkit

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

like image 504
mm24 Avatar asked May 28 '15 10:05

mm24


People also ask

How does the crown work on Apple Watch?

The Digital Crown is is the rotating button on the side of Apple Watch. Press and hold the Digital Crown to use Siri, turn it to scroll or zoom, press it to view the Watch face or Home Screen, and double-press to open the last used app.

What is a haptic crown?

Apple Watch Series 4 and later provides haptic feedback for the Digital Crown, which gives people a more tactile experience as they scroll through content. By default, the system provides linear haptic detents — or taps — as people rotate the Digital Crown a specific distance.

What does side button on Apple Watch do?

Press the side button Press to show or hide the Dock. Press and hold to use SOS. Double-click to use Apple Pay. Press and hold to turn your watch on or off.


2 Answers

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))
    }
like image 172
Thilo Avatar answered Sep 25 '22 02:09

Thilo


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
    }
}
like image 21
kelin Avatar answered Sep 22 '22 02:09

kelin