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

发布于 2024-11-28 22:34:09 字数 924 浏览 1 评论 0原文

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

[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 {

通过设置 a 来

[UIView setAnimationDidStopSelector:@selector(shrinkImage)];

执行第二个动画但是有没有一种方法可以让我传入值(在本例中是一个名为 image6 的 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

- (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?

THANKS!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莳間冲淡了誓言ζ 2024-12-05 22:34:09

如果您想使用基于选择器的方法,您在 setAnimationDidStopSelector 中指定的选择器将传递您在 beginAnimations:context 中指定的 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);
    }];
}];

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.

So, if you change your beginAnimations call to:

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

then image6 will get passed as the context parameter to your selector.

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);
    }];
}];
ˉ厌 2024-12-05 22:34:09

这是我的代码中的一个示例。但你明白了。您需要使用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有单块动画 你可以使用它来代替两块动画。苹果文档指出,单块动画更流畅,而且很容易将多个动画一个接一个地拼接起来......

更新:经过更仔细的思考,你可以这样做 -

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

正如你所看到的,之后动画完成后,您可以开始另一个动画,也可以进行清理。这个基于块的动画用于将动画链接在一起......

This is an example from my code. But you get the idea. You need to use CGAffineTransformConcat to stitch multiple animation together. 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. Apple documentation states that Single Block animation is more smooth, plus its easy to stitch multiple animations one after the other...

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. This block based animation, is used to chain animations together...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文