I am trying to show a button only when user starts panning for 0.3 seconds, to avoid showing the button instantaneously in some cases (e.g. at the end of zooming if fingers are not lifted together). To achieve that, I start a Timer when pan gesture is in .began
state, then showing the button in .changed
state only when timer becomes nil.
However, my Timer never runs until I lift my finger (i.e. gesture is .ended
). I figured it probably has to do with run loop and gesture occupying the main thread? Any workaround would be appreciated. Thanks!
var timer: Timer?
func handler(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
if timer == nil {
// I just want timer to invalidate itself after firing, so nothing to execute
self.timer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: false, block: { _ in })
case .changed:
if button.isHidden && timer == nil {
button.isHidden = false
}
case .ended:
button.isHidden = true
timer?.invalidate()
timer = nil
default:
return
}
}