You need to map your subclass InterfaceControllerto WKCrownDelegateand implement the method crownDidRotate.
If you want your value to be in the range of 1 to 10, you just need to implement simple logic to check what it will cost when you add rotationalDelta, and if it is out of the range of 1-10, the value of the card is 1 or 10, in depending on which direction the new value will be exceeded. I suggested that you want to valuebe Int, if not, just delete the conversion rotationalDeltato Intand valuewill Double.
Keep in mind that a rotationalDeltaof 1.0 represents a complete rotation of the crown, and rotationalDeltachanges sign based on the direction of rotation.
class MyInterfaceController: WKInterfaceController, WKCrownDelegate {
var value = 1
@IBOutlet var label: WKInterfaceLabel!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
crownSequencer.delegate = self
}
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
let newValue = value + Int(rotationalDelta)
if newValue < 1 {
value = 1
} else if newValue > 10 {
value = 10
} else {
value = newValue
}
label.setText("Current value: \(value)")
}
}
source
share