Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User interaction with uiview and animation completion blocks

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.

like image 296
Justin Miller Avatar asked Mar 03 '11 13:03

Justin Miller


2 Answers

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];
like image 93
jammur Avatar answered Oct 18 '22 15:10

jammur


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

like image 30
Justin Miller Avatar answered Oct 18 '22 15:10

Justin Miller