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
}
}