What I am trying to achieve:
I am trying to execute two different tasks depending on if the user tapped the screen once or twice in the touchesBegan
method.
In the process of doing so, I am delaying the execution of the singleTapTask
by 0.3s to be able to cancel the DispatchWorkItem
belonging to the doubleTapTask
.
My approach:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let singleTapTask = DispatchWorkItem {
// single tap
}
let doubleTapTask = DispatchWorkItem {
// double tap
}
if touch.tapCount == 1 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: singleTapTask)
} else if touch.tapCount == 2 {
singleTapTask.cancel()
DispatchQueue.main.async(execute: doubleTapTask)
}
}
}
Issue:
For some reason the singleTapTask
is still executed alongside with the doubleTapTask
even though the singleTapTask
has been canceled.
I can not figure out why the singleTapTask
is executed in the first place and would highly appreciate your help :)