Reputation: 1750
I have a WKInterfaceButton which is used to increase value of weight user has selected. However the default behaviour is each time user presses the button the quantity increases by one, I want to achieve the effect where as long as the user keeps the button pressed, the quantity should increase and when user lifts the finger the final value of the quantity should be assigned to weights.
Touch events doesn't seem to be available on watchOS. I looked for WKGestureRecognizer but there are only four of them available, out of which none of them serve my purpose. How can I achieve this behaviour?
Upvotes: 1
Views: 412
Reputation: 5340
It sounds silly but you could use a WKLongPressGestureRecognizer
to do so.
In Interface builder add a WKLongPressGestureRecognizer
e.g. to a WKInterfaceImage
. Set the Min Duration from the WKLongPressGestureRecognizer
to 0 as in the screenshot below.
Now connect the action to your action in the InterfaceController. I am using a Timer to trigger the increment but you can do this with perform(_ aSelector: Selector, with anArgument: Any?, afterDelay delay: TimeInterval)
and a flag (button pressed) as well.
Consider that movements will be detected as well but this will not affect the functionality as required.
var gestureTimer:Timer?
@IBAction func gesture(_ sender: WKLongPressGestureRecognizer) {
switch sender.state {
case .began:
print("began")
gestureTimer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: { (timer) in
print("do something")
})
case .cancelled, .ended:
print("other")
if let timer = gestureTimer {
timer.invalidate()
gestureTimer = nil
}
default:
print("default")
}
}
Upvotes: 3