UIGestureRecognizer Tutorial: Getting Started

yep itā€™s working nowā€¦ thanks for the great tutorialā€¦

1 Like

Hey Patrick,
I was having that same exact issue, but found a solution. Itā€™s super easy, too!

Look for the handlePan function. Instead of:
let translation = recognizer.translationInView(self.view)
use this:
let translation = recognizer.translationInView(self.superview)

and if youā€™re using Swift 3:
let translation = recognizer.translation(in: self.superview)

hope that helps!

Can you tap on an image, and the image says a word (Iā€™m assuming using AVSpeechSynthesizer)?

I havenā€™t done that for a very long while - pre AVSpeechSynthesizer.

Thereā€™s an older tutorial here: https://www.raywenderlich.com/64623/make-narrated-book-using-avspeechsynthesizer-ios-7

Hi Caroline, is it possible to have a UIGestureRecognizer everywhere in an APP?
I have many Views in my App and i need to detect globaly the double tap with 2 fingers but without a new recognizer in ervery view.
I tried [myTap addTarget:[[UIApplication sharedApplication]keyWindow] action:@selector(handleMyTap:)];
but it doesnt work.

@udo - a UIGestureRecognizer is attached to one view and one view alone.

I havenā€™t done this myself, but you could experiment with implementing hitTest(_:with:) on your root UIView.

hitTest(_:with:) will intercept all touches on a window. You can then reject the touch by returning nil or return the view that is touched.

Not sure how that would go with a double tap though. You might have to do some further research.

Hi @caroline ! Thanks for the tutorial, itā€™s really helpful.
Iā€™m trying to implement it in swift 3 but I get stuck in the rotation transformation. Since

view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
recognizer.rotation = 0 

itā€™s not working anymore, I think that it should be replaced by

view.transform = view.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0

But this just doesnā€™t work. The image shakes a little bit, but it doesnā€™t rotate. Is like the code is not adding radiands to the previous transform (what I tried to do) but starting again each time.
If i take off ā€œrecognizer.rotation = 0ā€, as you said, crazy things happens.
Do you know what could be going on?
Thanks!

Hi again. I just wanted you to know that I have finally figured it out.
What happened was that my first aproach to replace the old code with the Swift 3 code was:

view.transform = CGAffineTransform(rotationAngle: recognizer.rotation)

and

view.transform = CGAffineTransform(scaleX: recognizer.scale, y: recognizer.scale)

Then I realized that this was creating a new CGAffineTransform each time, so I started trying with the other aproach (the correct one) in the Rotation code, BUT NOT in the Scale codeā€¦ So, since ā€œshouldRecognizeSimultaneouslyā€¦ā€ was set to true, when I was testing rotation, I was also (unwittingly) scaling the image, so the wrong code was called in the Rotation code restarting from time to time the transform property of the viewā€¦

Now I can sleep well :blush:
Thanks again for the tutorial!

1 Like

@msastre73 - Iā€™m glad you figured it out, and thank you very much for coming back and posting your solution :].

Thank you Caroline, for great tutorial.

Works perfect. But what if I use it with MKAnnotationView? Actually I need to update annotation coordinates since pan gesture doesnā€™t do it. First I tried to use native isDragable=true property of MKAnnotationView. It also work, but I still canā€™t detect the coordinates changing while the dragging process, exactly while I hold my finger on annotation view and drag. So I decided to use this tutorial approach, but here is another problem. Maybe you could save me a couple of weeks and help. Thank you!

@zhydanovajulia - I havenā€™t done this myself. You could try the solutions here: ios - MKAnnotationView and tap detection - Stack Overflow