User interaction with uiview and animation complet

2019-02-13 15:35发布

问题:

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.

回答1:

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];


回答2:

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



回答3:

Do you see the same behaviour if you use:

+ [UIView setAnimationDidStopSelector:]

instead of using blocks?