9

Just wondering is there way to have a view recognize tap gestures while it is being animated? I am working on a view that has a cashapelayer line tethered to it. When the user pans the view (pan gesture) the line follows accordingly until the user stops panning. At this point an animation is executed that brings the view back to its original position and the tether layer back as well. Now my only real problem is that while the view and the tether are animating the view doesnt respond to tap gestures…

Anyone know some tricks? I hope my explanation was understandable and thanks in advance!

(if the tethered view concept is not clear there is a free app called discovr apps which will give an example).

gcamp
  • 14,622
  • 4
  • 54
  • 85
johnnyd
  • 101
  • 1
  • 3
  • 1
    UPDATE: To clarify, the original bounding rectangle of the view DOES recognize the tap gesture but doesn't recognize the tap gesture of the moving view, in other words, the current frame. – johnnyd Sep 07 '11 at 16:39
  • You need to check this answer: http://stackoverflow.com/a/4432546/129202 – Jonny Dec 11 '12 at 08:16
  • The solution in the above link may work with raw touch handling but might not work with gestures. See this workaround for gestures: http://stackoverflow.com/questions/8340329/how-to-to-make-gesturerecognizer-working-in-an-animating-uiimage-view – Matti Jokipii Jan 20 '15 at 18:54
  • [Here](https://stackoverflow.com/a/66824606/885189) is an answer that might work for you. – JaredH Mar 26 '21 at 21:46

3 Answers3

15

I'm assuming that you are using the [UIView animateWithDuration: delay: options: animations: completion:]; method of animating.

If so, you need to pass UIViewAnimationOptionAllowUserInteraction as an option to get the animated view to respond to touches while it is animating.

mmc
  • 17,354
  • 2
  • 34
  • 52
  • 1
    Thanks for the answer but I'm already doing that. It seems that the gesture is recognized but only in the original rectangle of the view, not in the immediate moving view...I will update my question so that is more clear – johnnyd Sep 07 '11 at 16:38
5

(Swift 3) Pass .allowUserInteraction option

UIView.animate(withDuration: 0.75, delay: 0.0, options: [.allowUserInteraction], animations: {
      // Desired animation(s) 
}, completion: { (finished: Bool) in
        // Completion
})
ALamp
  • 181
  • 2
  • 5
3

You need to set two options - UIViewAnimationOptionAllowUserInteraction and UIViewAnimationOptionAllowAnimatedContent. First lets you interact with views during animation, second forces to redraw views on every frame of animation and not use snapshots of beginning and ending frames.

Kyr Dunenkoff
  • 8,090
  • 3
  • 23
  • 21
  • I tried your suggestion but still no dice. :( I might try this approach:[user-interaction-disabled](http://stackoverflow.com/questions/7221688/caanimation-user-input-disabled) because this is driving me crazy! :) – johnnyd Sep 07 '11 at 17:03
  • Hm. How about you try adding gesture recognizer to a superview of your animated view and then check if you tapped inside animated view with `hitTest:`? – Kyr Dunenkoff Sep 07 '11 at 17:08
  • Tried that before all of this and tried it again just for the sake of trying and still doesn't work...I just don't get it...thanks though for the suggestions! – johnnyd Sep 07 '11 at 17:26
  • a combination of options: [.allowUserInteraction, .allowAnimatedContent] works for me, thanks! – Zaur_M Aug 15 '21 at 13:07