How to recognize tap gesture while a view is anima

2020-06-15 12:34发布

问题:

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

回答1:

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.



回答2:

(Swift 3) Pass .allowUserInteraction option

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


回答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.