I want to implement longpressgesture when my touchbegan method there. I am using NSTimer and record the counter when counter became 3 . I recognized that is long press. But when i release the button and then again press mycounter persist previous value and increment on previous value .Although I am assign counter egual to zero . Please help any help would be apperciated.
var counter : Int = 0
var timer :NSTimer?
override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer = NSTimer()
counter == 0;
timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
func updateCounter() {
print(counter++)
if (counter > 3){
timer!.invalidate()
timer = nil;
}
}
override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer!.invalidate()
timer = nil;
counter == 0;
}
override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (counter>=3){
print("hello")
}
}
That's the classic equation ==
versus assign =
operator confusion
timer = NSTimer() // this line is actually not needed
counter = 0
...
timer?.invalidate() // better use question mark to avoid a potential crash
timer = nil
counter = 0
and remove all semicolons.