Get WatchOS Digital Crown value

I did some research on the Apple Watch apps, but I'm having trouble getting Digital Crown value. I looked at the WKCrownSequencer, but not sure what to do with it. Can someone show me how I'm going to get a variable with values ​​of 1-10 that change when the digital crown is rotated. Thank!

+4
source share
2 answers

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)")
    }
}
0
source

After WatchOS3, you can take the value of Digital Crown. Check out this documentation on Apple.

WKCrownDelegate . :

- (void)willActivate {
    [super willActivate];
    self.crownSequencer.delegate = self;
    [self.crownSequencer focus];
}

- (void)crownDidRotate:(WKCrownSequencer *)crownSequencer rotationalDelta:(double)rotationalDelta {
    self.totalDelta = self.totalDelta + rotationalDelta;
    [self updateLabel];
}

- (void)updateLabel {
    [self.label setText:[NSString stringWithFormat:@"%f", self.totalDelta]];
}

rotationDelta​​h3 >

1.0 , .

. , , . Apple.

0

Source: https://habr.com/ru/post/1683869/


All Articles