ios - swift: when I open the view controller and press the gesture recognizer for the first time it says that i pressed it twice. how to fix this? -


when open view controller have 4 gesture recognizers, when tap twice shout happen:

@iboutlet var doubletap: uitapgesturerecognizer! @ibaction func doubletapaction(sender: anyobject) {     doubletap.numberoftapsrequired = 2     print("button 2x") } 

but when press 1 of 4 buttons first time(i pressed 1 time.) prints tapped twice. after first time works fine.

can tell how fix this?

thanks reading question hope can me.

if don't understand question please comment.

this because first time gesture recognizer set recognize 1 single tap. change 2 taps, set in wrong place, because tapped once, enters action , executed code there:

self.doubletap.numberoftapsrequired = 2 // wrong here! print("button 2x") 

so changes recognizer recognize 2 taps, recognized 1 (that's why code gets executed) prints text well.

it makes no sense set every time tap, need once. place viewdidload.

you set number of required taps in interface builder.

code example:

@iboutlet var doubletap: uitapgesturerecognizer! @ibaction func doubletapaction(sender: anyobject) {     print("doubletap 2x") }  override func viewdidload() {     super.viewdidload()      self.doubletap.numberoftapsrequired = 2 } 

Comments