简体   繁体   English

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

[英]how to execute multiple animations in a row on the same UIImageView

I'd like to do two animations, one after the other:我想做两个动画,一个接一个:

[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];

I know that I can do the second animation in我知道我可以做第二个 animation

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

or by setting a或通过设置

[UIView setAnimationDidStopSelector:@selector(shrinkImage)];

But is there a way that I can pass in the value (in this case a UIIMageView named image6) so that the second animation occurs on the same object as the first one?但是有没有一种方法可以传递值(在这种情况下是一个名为 image6 的 UIIMageView),以便第二个 animation 出现在与第一个相同的 object 上?

THANKS!谢谢!

If you want to use the selector-based approach, the selector that you specify in the setAnimationDidStopSelector gets passed a context parameter that you specify in your beginAnimations:context: call.如果您想使用基于选择器的方法,您在setAnimationDidStopSelector中指定的选择器将传递您在beginAnimations:context:调用中指定的context参数。

So, if you change your beginAnimations call to:因此,如果您将 beginAnimations 调用更改为:

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

then image6 will get passed as the context parameter to your selector.然后image6将作为上下文参数传递给您的选择器。

An easier way would be to use blocks to accomplish the same thing:更简单的方法是使用块来完成同样的事情:

[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);
    }];
}];

This is an example from my code.这是我的代码中的一个示例。 But you get the idea.但你明白了。 You need to use CGAffineTransformConcat to stitch multiple animation together.您需要使用CGAffineTransformConcat将多个 animation 拼接在一起。 You can do this-你可以这样做-

   [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 has Single block animation you could use that instead of Two Block animation. iOS4 有单块 animation你可以用它代替两块 animation。 Apple documentation states that Single Block animation is more smooth, plus its easy to stitch multiple animations one after the other... Apple 文档指出,Single Block animation 更流畅,而且它易于拼接多个动画一个接一个...

UPDATE: After thinking more carefully, you could do this -更新:经过仔细考虑后,您可以这样做-

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

As you can see, after the animation completes, you can start another animation or you can clean up.如您所见,在 animation 完成后,您可以启动另一个 animation 或者您可以清理。 This block based animation, is used to chain animations together...这个基于块的 animation 用于将动画链接在一起......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM