How to recognize tap gesture while a view is animating
Asked Answered
L

3

10

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).

Lemur answered 7/9, 2011 at 16:30 Comment(4)
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.Lemur
You need to check this answer: https://mcmap.net/q/1161722/-uiview-touchesbegan-doesn-39-t-respond-during-animationAgatha
The solution in the above link may work with raw touch handling but might not work with gestures. See this workaround for gestures: #8340829Vshaped
Here is an answer that might work for you.Speak
R
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.

Retrad answered 7/9, 2011 at 16:35 Comment(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 clearLemur
C
6

(Swift 3) Pass .allowUserInteraction option

UIView.animate(withDuration: 0.75, delay: 0.0, options: [.allowUserInteraction], animations: {
      // Desired animation(s) 
}, completion: { (finished: Bool) in
        // Completion
})
Crag answered 25/7, 2017 at 0:9 Comment(0)
I
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.

Interlard answered 7/9, 2011 at 16:46 Comment(4)
I tried your suggestion but still no dice. :( I might try this approach:user-interaction-disabled because this is driving me crazy! :)Lemur
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:?Interlard
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!Lemur
a combination of options: [.allowUserInteraction, .allowAnimatedContent] works for me, thanks!Zymosis

© 2022 - 2024 — McMap. All rights reserved.