Enable user interaction for gesture recognizer during UIView.animateWithDuration()

1.1k views Asked by At

I have a view (A) that holds several rectangular subviews (B). Each of these subviews have a single tap recognizer to trigger an action. The parent view A also has a single tap recognizer that calls a function on A's controller to make each of the subviews B flash in a color. This is the function:

@IBAction func highlightAreas(recognizer: UITapGestureRecognizer) {
    for area in buttons {
        // only show linked areas
        if area.targetPage != nil {
            let oldColor = area.backgroundColor

            // show areas with animation
            UIView.animateWithDuration(HIGHLIGHT_ANIMATION_TIME, animations: { Void in  // begin of closure
                area.backgroundColor = self.HIGHLIGHT_BACKGROUND_COLOR.colorWithAlphaComponent(self.HIGHLIGHT_ALPHA)
            })  // end of closure

            // hide areas with animation
            UIView.animateWithDuration(HIGHLIGHT_ANIMATION_TIME, animations: { Void in  // begin of closure
                area.backgroundColor = oldColor?.colorWithAlphaComponent(0.0)
            })  // end of closure
        }
    }
}

It works but during the animation the subviews B won't trigger their single tap event. How can I make it possible to detect that single tap during the animation?

1

There are 1 answers

0
luk2302 On BEST ANSWER

You have to use

animateWithDuration(_ duration: NSTimeInterval,
                         delay: NSTimeInterval,
                       options: UIViewAnimationOptions,
                    animations: () -> Void,
                    completion: ((Bool) -> Void)?)

for that. And provide AllowUserInteraction as an option to allow the user to interact with the view during animation.
See the docs for more options.