iPhone:如何陆续提交两个动画 [英] iPhone: How to commit two animations after another

查看:128
本文介绍了iPhone:如何陆续提交两个动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个绝对的初学者提出的问题(对不起),但我想知道如何承诺1动画和一旦结束开始另一个。想象一下,有一个图像从x = 0移到X = 300。然后你要再做动画相同的图像,但这次从X = 300为x = 330,使其从屏幕上消失。

以下code只会做从X = 300 x = 330的动画不会犯动画X = 0到x = 300。我敢肯定,我没有得到commitAnnimation的概念,并认为这是显而易见的,但我怎么会一个接一个做两动画?

我知道我可以在图像通俗易懂移动到330,但我不希望这是我所需要的第一个动画(X 0 - 300)是与其他动画同步。

下面是我的(错误)code:

  [UIView的beginAnimations:@shadowMove背景:无]; //开始动画
[UIView的setAnimationDuration:0.5];
[UIView的setAnimationCurve:UIViewAnimationCurveEaseInOut];[ImageView的SETFRAME:CGRectOffset([ImageView的框架],300,0)];[UIView的commitAnimations]; //动画结束
//第二动画
[UIView的beginAnimations:@shadowMoveRestAway背景:无]; //开始动画
[UIView的setAnimationDuration:0.5];
[UIView的setAnimationCurve:UIViewAnimationCurveEaseInOut];[ImageView的SETFRAME:CGRectOffset([ImageView的框架],330,0)]; //移动ImageView的关闭屏幕[UIView的commitAnimations]; //动画结束


解决方案

使用委托方法

  [UIView的setAnimationDidStopSelector:@selector(animationDidStop:完成:上下文:)];

在第一动画结束别的东西开始。所以,把你想要的东西旁边的animationDidStop里面做:完成:上下文方法,记得iPhone是一个事件驱动的环境,使线性code就像你有上面会简单地揭开序幕每个动画几乎在同一时间

编辑:

我忘了补充一点,你需要设置动画的代表以及否则你不会得到该事件时,第一个停止 - 见下文;

这是你的code的完整版本的改变 - 我使用的缩写animationDidStop代表因为这是更容易理解和适用于本示例

 <$ C C> [UIView的beginAnimations:@shadowMove背景:无]; //开始动画
[UIView的setAnimationDuration:0.5];
[UIView的setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView的setAnimationDelegate:个体经营];
[UIView的setAnimationDidStopSelector:@selector(myFirstAnimationDidStop)];
[ImageView的SETFRAME:CGRectOffset([ImageView的框架],300,0)];
[UIView的commitAnimations]; //动画结束

然后你只需要像这样的新方法;

   - (无效){myFirstAnimationDidStop
//第二动画[UIView的beginAnimations:@「shadowMoveRestAway背景:无];
[UIView的setAnimationDuration:0.5];
[UIView的setAnimationCurve:UIViewAnimationCurveEaseInOut];
[ImageView的SETFRAME:CGRectOffset([ImageView的框架],330,0)];
[UIView的commitAnimations];
}

和完整性,在接口(.h)文件中,你应该添加;

- (无效)myFirstAnimationDidStop;

@选择很容易,它只是一个指向另一种方法的方式 - 希望这个例子阐明了

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

解决方案

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.

这篇关于iPhone:如何陆续提交两个动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆