如何在同一个UIImageView上连续执行多个动画

时间:2011-08-10 16:54:39

标签: iphone animation uiimageview

我想一个接一个地做两个动画:

[UIView beginAnimations:@"growImage" context:nil];
[UIView setAnimationDuration:0.2f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];

[UIView beginAnimations:@"shrinkImage" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
image6.transform = CGAffineTransformMakeScale(0.1, 0.1);
[UIView commitAnimations];

我知道我可以在

中做第二个动画
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

或设置

[UIView setAnimationDidStopSelector:@selector(shrinkImage)];

但有没有一种方法可以传递值(在本例中是一个名为image6的UIIMageView),以便第二个动画出现在与第一个动画相同的对象上?

谢谢!

2 个答案:

答案 0 :(得分:2)

如果您想使用基于选择器的方法,您在setAnimationDidStopSelector中指定的选择器会传递您在context调用中指定的beginAnimations:context:参数。

因此,如果您将beginAnimations调用更改为:

[UIView beginAnimations:@"growImage" context:image6];

然后image6将作为上下文参数传递给您的选择器。

更简单的方法是使用块来完成同样的事情:

[UIView animateWithDuration:0.2 animations:^() {
    image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
}
completion:^(BOOL finished) {
    [UIView animateWithDuration:0.2 animations:^() {
        image6.transform = CGAffineTransformMakeScale(0.1, 0.1);
    }];
}];

答案 1 :(得分:1)

这是我的代码中的一个例子。但是你明白了。您需要使用CGAffineTransformConcat将多个动画拼接在一起。你可以这样做 -

   [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    angle             = 0;
    scaleFactor       = 1;
    startPoint.x      = 60.0;
    startPoint.y      = 60.0;
    CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
    CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI/180);
    boxView.center    = startPoint;
    boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);
    [UIView commitAnimations];

iOS4有Single block animation你可以使用它而不是Two Block动画。 Apple文档声明单块动画更加流畅,并且可以轻松拼接多个动画......

更新:在仔细考虑之后,你可以这样做 -

[UIView animateWithDuration:0.3 
                      delay:0 
                    options:UIViewAnimationCurveEaseOut 
                 animations:^{ /* first level of animation */
                 } 
                 completion:^(BOOL finished){
                 }
];

如您所见,动画完成后,您可以启动另一个动画,也可以清理。这个基于块的动画,用于将动画链接在一起......