iPhone: How to commit two animations after another

余生长醉 提交于 2019-12-06 15:45:38

Use the delegate method

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

to start something else when the first animation has finished. So put what you want to do next inside the animationDidStop:finished:context method, remember that the iphone is an event driven environment so linear code like you have above will simply kick off each animation at almost the same time.

EDIT:

I forgot to add that you need to set the animation delegate as well otherwise you won't get the event when the first one stops - see below;

Here's a full version of your code with the change - I'm using the abbreviated animationDidStop delegate as that's easier to understand and fine for this example.

[UIView beginAnimations:@"shadowMove" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(myFirstAnimationDidStop)];
[imageView setFrame:CGRectOffset([imageView frame], 300, 0)]; 
[UIView commitAnimations]; // End animations

Then you just need a new method like this;

-(void) myFirstAnimationDidStop {
// Second Animation

[UIView beginAnimations:@"shadowMoveRestAway" context:nil]; 
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 330, 0)];
[UIView commitAnimations]; 
}

And for completeness, in your interface (.h) file you should add;

-(void) myFirstAnimationDidStop;

@selector is easy, it's just a way of pointing to another method - hopefully this example clarifies that.

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