User interaction with uiview and animation completion blocks
Asked Answered
R

3

6

I have the following code:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
     animations:^{
         imageView.bounds = endBounds;
     }
     completion:^(BOOL finished) {
         [UIView animateWithDuration:2.0 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction
              animations:^{
                  imageView.bounds = startBounds;
              }
              completion:^(BOOL finished) {
                      [imageView removeFromSuperview];
              }];
     }];

Additionally I have:

[imageView setUserInteractionEnabled:YES];

and a tap gesture recognizer set that will handle the user tapping on imageView. While the first animation is happening, the gesture recognizer fires as I would expect. But if I try and tap imageView during the chained animation from the completion block, nothing happens even though I have set the appropriate option.

Anyone have any thoughts? I've googled and can't find an answer.

Read answered 3/3, 2011 at 13:19 Comment(2)
Sorry for the poor code formatting...Read
welcome at SO. I add iphone tag to your question(fix it if I was wrong), in the future try first to put tags which describe platform and programming language, and after some specific tagsEnwind
R
5

I came up with a solution:

I wrap the UIImageView in a UIView (I subclass UIView) with the same bounds/center point as the image. Then I attach the gesture recognizer to the wrapper, instead of the image. Because the wrapper's bounds rectangle/center point never change for the duration of the animation, it's always available as the target of a gesture.

This works quite well.

-j

Read answered 7/3, 2011 at 17:23 Comment(1)
This is literally the only solution to this day. I'm using a UIViewPropertyAnimator and after animating a view with a gesture recognizer attached to it, it loses it's gesture recognizer for some reason. The only workaround is to put the view you want to animate in a container view and add a gesture recognizer to that container view. Then you animate the view (not the container view).Irreverence
H
28

When using the new animation blocks, if you want user interaction to be enabled during the animation, you have to set it in the options mask. For example:

[UIView animateWithDuration:1.0 
                      delay:0 
                    options:UIViewAnimationOptionAllowUserInteraction 
                 animations:^{ myView.alpha = 0.5; } 
                 completion:NULL];
Haerle answered 6/4, 2011 at 20:45 Comment(1)
When delay is not 0, it cannot receive touches.Mccartan
R
5

I came up with a solution:

I wrap the UIImageView in a UIView (I subclass UIView) with the same bounds/center point as the image. Then I attach the gesture recognizer to the wrapper, instead of the image. Because the wrapper's bounds rectangle/center point never change for the duration of the animation, it's always available as the target of a gesture.

This works quite well.

-j

Read answered 7/3, 2011 at 17:23 Comment(1)
This is literally the only solution to this day. I'm using a UIViewPropertyAnimator and after animating a view with a gesture recognizer attached to it, it loses it's gesture recognizer for some reason. The only workaround is to put the view you want to animate in a container view and add a gesture recognizer to that container view. Then you animate the view (not the container view).Irreverence
G
0

Do you see the same behaviour if you use:

+ [UIView setAnimationDidStopSelector:]

instead of using blocks?

Gratuitous answered 3/3, 2011 at 14:5 Comment(3)
I haven't tried that. That's the old style, right? This is my first foray into iPhone development and I figured I would try the recommended approach first with blocks. I can give that a go later on.Read
Btw, I should have mentioned that startBounds is the original rectangle from when the image was first loaded and endBounds has the same dimensions except for a height of 0 (so the image disappears). Is it possible that, upon completion, the sdk sees a bounding box with a height of 0, assumes the image is no longer visible and disables all user interaction?Read
Update: the same behavior occurs -- in that no gestures are fired.Read

© 2022 - 2024 — McMap. All rights reserved.