ios - Cancelling a DispatchWorkItem waiting for execution in DispatchQueue -
what trying achieve:
i trying execute 2 different tasks depending on if user tapped screen once or twice in touchesbegan
method.
in process of doing so, delaying execution of singletaptask
0.3s able cancel dispatchworkitem
belonging doubletaptask
.
my approach:
override func touchesbegan(_ touches: set<uitouch>, event: uievent?) { 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 reason singletaptask
still executed alongside doubletaptask
though singletaptask
has been canceled. can not figure out why singletaptask
executed in first place , highly appreciate :)
a simple approach add 2 uitapgesturerecognizer
view (singletapgesturerecognizer
, doubletapgesturerecognizer
).
the first 1 configured singletapgesturerecognizer.numberoftapsrequired = 1
, second 1 doubletapgesturerecognizer.numberoftapsrequired = 2
.
now add singletapgesturerecognizer.require(tofail: singletapgesturerecognizer)
furthermore, approach above not targeting sequential single , double taps, number of taps @ 1 tap (number of fingers tapping @ same time)
Comments
Post a Comment