bounce an image in to view

2019-09-07 14:35发布

问题:

I have some code which works nicely and acts like a menu appearing when you click a button. I just want to animate slightly. So when it appears, it appears to bounce in. Here's the code which shows it

[UIView beginAnimations:@"theAnimation" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(yourAnimationHasFinished:finished:context:)];
[UIView setAnimationDuration: 0.3];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0, -20);
scrollChangeClothing.transform = moveUp;
imgClothesMenuBg.transform = moveUp;
[UIView commitAnimations];  

[UIView beginAnimations:@"theAnimation" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(yourAnimationHasFinished:finished:context:)];
[UIView setAnimationDuration: 0.1];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0, 0);
scrollChangeClothing.transform = moveUp;
imgClothesMenuBg.transform = moveUp;
[UIView commitAnimations];  

the code above (I thought) should first show the image at -20 pixels start point, then settle back to it's original point.

I tried combining the transform commands but that didnt work either. Do I need to put the second animation in the setAnimationDidStopSelector?? So once it's finished, it will then bounce?

回答1:

I think your code doesn't work because the two animation blocks are coalesced behind the scene into one identity transform.

  • Try removing the UIView animation methods from the first block, leaving only the lines below. This should make your views "slide into position".

    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0, -20); scrollChangeClothing.transform = moveUp; imgClothesMenuBg.transform = moveUp;

  • Try enclosing both blocks in an outer +beginAnimations:context:/+commitAnimations block if you do need to animate "sliding out".



回答2:

Yes, you should move your second animation block into the first's animationDidStop method.