I found some good code on another stack overflow question for this, however my single tap code is running when I do a single tap or a double tap. heres the code (by the way, double tap meaning I tap once, and within 0.3 seconds I tap again, not 2 fingers simultaneously tapping)
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch: AnyObject? = touches.anyObject()
if (touch?.tapCount == 2) {
NSObject.cancelPreviousPerformRequestsWithTarget(self)
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let touch: AnyObject? = touches.anyObject()
if (touch?.tapCount == 1) {
let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
println("this runs only if single tap")
})
} else if (touch?.tapCount == 2) {
println("double tap touches ended")
}
}
I thought that the NSObject.cancelPreviousPerformRequestsWithTarget(self)
is supposed to stop the single tap block from running, however my println("this runs only if single tap")
is still running when I double tap. First my double tap runs, then after 0.3 seconds the single tap code runs too.. any ideas on what I'm doing wrong?