I have a WatchOS app with scrollable views. Depending on the state of the app I would like to receive crown rotation events.
To achieve this behavior I have set up a timer that calls the focus() method of the crownSequencer.
In the Apple Watch simulator in XCode the app behaves consistently and there are no problems. However, some users (not all) report that rotating the crown does not work. What could be the reason for this behavior?
Here is the relevant code:
import WatchKit
import Foundation
import HealthKit
class MyInterfaceController: WKInterfaceController {
var controlTimer: Timer?;
var restTimer: Timer?;
var timer: Timer?;
override func awake(withContext context: Any?) {
super.awake(withContext: context)
setupTimers()
}
override func willActivate() {
super.willActivate()
setupTimers()
}
override func didAppear() {
super.didAppear()
setupTimers()
}
func setupTimers() {
clearTimers()
restTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { _ in
// Do something
});
timer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { _ in
// Do something
});
controlTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: { _ in
if (someCondition == false) {
self.crownSequencer.resignFocus()
} else {
self.crownSequencer.delegate = self
self.crownSequencer.focus()
}
});
}
func clearTimers() {
controlTimer?.invalidate()
controlTimer = nil
restTimer?.invalidate()
restTimer = nil
timer?.invalidate()
timer = nil
}
}
extension MyInterfaceController : WKCrownDelegate {
func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) {
if (rotationalDelta > 0.2) {
// Do something
} else if (rotationalDelta < -0.2) {
// Do something
}
}
}