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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Justin Miller
  • 757
  • 2
  • 8
  • 19

3 Answers3

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];
jammur
  • 1,189
  • 3
  • 10
  • 18
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

Justin Miller
  • 757
  • 2
  • 8
  • 19
  • 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). – Saoud Rizwan Jan 14 '17 at 11:38
0

Do you see the same behaviour if you use:

+ [UIView setAnimationDidStopSelector:]

instead of using blocks?

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • 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. – Justin Miller Mar 03 '11 at 14:28
  • 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? – Justin Miller Mar 04 '11 at 04:44
  • Update: the same behavior occurs -- in that no gestures are fired. – Justin Miller Mar 06 '11 at 07:02