Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulty allowing user interaction during UIView animation

I'm struggling to figure out how to allow user interaction with a view as it's being animated.

Here's the situation: I have a UIView cardView which holds card subviews. The cards are draggable tiles, similar to how the cards in Tinder are draggable/swipeable.

I am trying to fade out the card using animateWithDuration by animating to cardView.alpha = 0. Logically, this will also fade out all of the subviews (card objects). In this specific case, I am only targeting one card subview. However, during the animation, I am unable to drag/interact with the card.

Here is the code I'm using:

UIView.animateWithDuration(
        duration,
        delay: 0,
        options: UIViewAnimationOptions.AllowUserInteraction,
        animations: {self.cardView.alpha = 0}
    ) {
        _ in
        println("Card faded out")
        card.removeFromSuperview()
    }

Why doesn't this work? Any help will be appreciated. Thank you!!

like image 944
ambient Avatar asked Aug 24 '14 23:08

ambient


People also ask

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

Is UIView animate asynchronous?

UIView. animate runs on the main thread and is asynchronous.


3 Answers

I think you can find the answer in this previous post.

The interesting bit of the post is:

UIView's block animation by default blocks user interaction, and to get around it you need to pass UIViewAnimationOptionAllowUserInteraction as one of the options.

like image 189
gabriel_101 Avatar answered Oct 03 '22 07:10

gabriel_101


I fixed this problem by setting alpha to 0.1 instead of 0.0. I'm not sure if that will work in your case, but it shows that the event handling code thought that the view was not visible and disabled interaction even with the UIViewAnimationOptionAllowUserInteraction flag set. Oddly, setting the alpha to 0.01 did not work, so there is a threshold of visibility you have to stay above.

like image 20
EricS Avatar answered Oct 03 '22 06:10

EricS


Swift 5

UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
        self.customButton.backgroundColor = .none
    }, completion: nil)
like image 28
Elshad Karimov Avatar answered Oct 03 '22 07:10

Elshad Karimov