1

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?

duxfox--
  • 8,314
  • 13
  • 50
  • 112

1 Answers1

0

the problem was that

NSObject.cancelPreviousPerformRequestsWithTarget(self)

does not cancel a

dispatch_after(dispatchTime, dispatch_get_main_queue()

therefore this will never work. See my other question for further details about this

Community
  • 1
  • 1
duxfox--
  • 8,314
  • 13
  • 50
  • 112