iPhone:如何连续提交两个动画
这绝对是初学者的问题(抱歉),但我想知道如何提交一个动画,并在它结束后开始另一个动画。想象一下将图像从 x=0 移动到 x=300。然后您想要再次对同一图像进行动画处理,但这次从 x=300 到 x=330,以便它从屏幕上消失。
以下代码只会执行从 x=300 到 x=330 的动画,不会提交 x=0 到 x=300 的动画。我确信我不明白 commitAnimation 的概念,而且这是显而易见的,但是我如何一个接一个地制作两个动画呢?
我知道我可以将图像直接移动到 330,但我不希望这样做,因为我需要第一个动画 (x 0 - 300) 与另一个动画同步。
这是我的(错误的)代码:
[UIView beginAnimations:@"shadowMove" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 300, 0)];
[UIView commitAnimations]; // End animations
// Second Animation
[UIView beginAnimations:@"shadowMoveRestAway" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 330, 0)]; // Move imageView off screen
[UIView commitAnimations]; // End animations
This is an absolute beginner's question (sorry), but I was wondering how to commit one animations and once it has ended to start another one. Imagine having an image moved from x=0 to x=300. Then you want to do animate the same image again, but this time from x=300 to x=330 so that it disappears from the screen.
The following code will only do the animation from x=300 to x=330 and will not commit the animation x=0 to x=300. I'm sure I don't get the concept of commitAnnimation and that this is obvious, but how would I do two animations after one another?
I know I could move the image straightaway to 330, but I don't want this as I need the first animation (x 0 - 300) to be in sync with another animation.
Here is my (wrong) code:
[UIView beginAnimations:@"shadowMove" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 300, 0)];
[UIView commitAnimations]; // End animations
// Second Animation
[UIView beginAnimations:@"shadowMoveRestAway" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 330, 0)]; // Move imageView off screen
[UIView commitAnimations]; // End animations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当第一个动画完成时,使用委托方法
开始其他操作。因此,将您接下来想要执行的操作放入animationDidStop:finished:context 方法中,请记住,iphone 是一个事件驱动的环境,因此像上面这样的线性代码将几乎同时启动每个动画。
编辑:
我忘记补充一点,您还需要设置动画委托,否则当第一个停止时您将不会收到事件 - 见下文;
这是经过更改的代码的完整版本 - 我使用缩写的animationDidStop委托,因为这更容易理解并且适合本示例。
那么你只需要一个像这样的新方法;
为了完整起见,您应该在接口 (.h) 文件中添加;
-(void) myFirstAnimationDidStop;
@selector 很简单,它只是指向另一个方法的一种方式 - 希望这个例子能够澄清这一点。
Use the delegate method
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.
Then you just need a new method like this;
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.