User interaction with uiview and animation completion blocks

帅比萌擦擦* 提交于 2019-11-30 04:56:38

问题


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?



来源:https://stackoverflow.com/questions/5181216/user-interaction-with-uiview-and-animation-completion-blocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!