首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对UIAnimation的理解

对UIAnimation的理解
EN

Stack Overflow用户
提问于 2011-11-02 15:31:45
回答 3查看 5.6K关注 0票数 1

我正在尝试使用下面的代码来执行一些动画

代码语言:javascript
复制
-(void) performSlidingfromX:(int) xx fromY:(int) yy 
{
UIImageView *Image= [self getImage];

[UIView beginAnimations:nil context:NULL];  
[UIView setAnimationDuration: 1.0];
[UIView setAnimationBeginsFromCurrentState:true];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[token setFrame:CGRectMake(xx, yy, 64, 64)];
[UIView commitAnimations];

}

我就像在for循环中一样调用

代码语言:javascript
复制
for (i = 0; i < totMoves; i++) {
    Moment *m = [moments objectAtIndex:i];
    int xx= [m X];
    int yy= [m Y];

    [self performSlidingfromX:xx fromY:yy];

}

我面临的问题是,它会动画到最终位置,例如,如果我为xx,yy输入以下时刻

代码语言:javascript
复制
0,0
50,0
50,50

它将图像从0,0对角线移动到50,50,我希望它先水平滑动,然后垂直滑动。

有什么帮助吗?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-02 15:47:40

使用新的块动画。它简单且稳定:

代码语言:javascript
复制
[UIView animateWithDuration:0.5 
                          delay:0 
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         [token setFrame:CGRectMake(xx, 0, 64, 64)];
                         //here you may add any othe actions, but notice, that ALL of them will do in SINGLE step. so, we setting ONLY xx coordinate to move it horizantly first.
                     } 
                     completion:^(BOOL finished){

                         //here any actions, thet must be done AFTER 1st animation is finished. If you whant to loop animations, call your function here.
                         [UIView animateWithDuration:0.5 
                                               delay:0 
                                             options:UIViewAnimationOptionBeginFromCurrentState 
                                          animations:^{[token setFrame:CGRectMake(xx, yy, 64, 64)];} // adding yy coordinate to move it verticaly} 
                                          completion:nil];
                     }];
票数 9
EN

Stack Overflow用户

发布于 2011-11-02 15:56:01

问题是你在for循环中不断调用"performSlidingfromX:xx fromY:yy“。尝试以下代码:

代码语言:javascript
复制
     i=0;
     Moment *m = [moments objectAtIndex:i];
     int xx= [m X];
     int yy= [m Y];
     [self performSlidingfromX:xx fromY:yy];

-(void) performSlidingfromX:(int) xx fromY:(int) yy 
{
i++;
[UIView beginAnimations:nil context:NULL];  
[UIView setAnimationDuration: 1.0];
[UIView setAnimationBeginsFromCurrentState:true];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
[token setFrame:CGRectMake(xx, yy, 64, 64)];
[UIView commitAnimations];


[self performSelector:@selector(call:) withObject:[NSNumber numberWithInt:i] afterDelay:1.1];

}
-(void)call
{
 Moment *m = [moments objectAtIndex:i];
     int xx= [m X];
     int yy= [m Y];
     [self performSlidingfromX:xx fromY:yy];
 }
票数 1
EN

Stack Overflow用户

发布于 2011-11-02 16:52:12

制作动画不是阻塞调用。您的代码不会停止并等待动画完成。在开始动画之后,循环的下一次迭代将立即运行。这将创建一个新动画,该动画会影响相同的属性,因此它将替换以前的动画。你得到的结果就好像你只运行了循环的最后一次迭代。

不幸的是,没有简单的代码块来完成您想要做的事情。您需要检测动画何时结束,然后开始下一个动画。您需要在比局部变量范围更广的范围内跟踪您的状态(主要是您所在的状态)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7977019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档