【问题标题】:how to execute multiple animations in a row on the same UIImageView如何在同一个 UIImageView 上连续执行多个动画
【发布时间】:2011-08-10 16:54:38
【问题描述】:

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

[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),以便第二个动画与第一个动画出现在同一个对象上?

谢谢!

【问题讨论】:

    标签: iphone animation uiimageview


    【解决方案1】:

    如果您想使用基于选择器的方法,您在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);
        }];
    }];
    

    【讨论】:

    • 完美 - 谢谢!我还必须更改 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 到 - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished上下文:(UIImageView *)上下文{
    【解决方案2】:

    这是我的代码中的一个示例。但你明白了。您需要使用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 你可以用它代替两块动画。 Apple 文档指出 Single Block 动画更加流畅,而且可以轻松拼接多个动画一个接一个...

    更新:经过仔细考虑,您可以这样做 -

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

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

    【讨论】:

    • 感谢您的评论。这看起来像您同时缩放和旋转。我正在寻找放大图像,然后当动画完成时,我想缩小图像 - 我不能像缩放和旋转那样同时发生增长和缩小......
    • 我喜欢这个基于块的动画的想法,但是在使用你的代码时我遇到了一个错误——初始化'void (^)()'的块指针类型不兼容,预期'void (^)(无效)'
    • 注意:我是徒手输入的。可能有错别字!请参考 Apple 文档或其他在线文档...很高兴我能提供帮助。
    • 谢谢 - 我将正确答案授予@highlycaffeinated,因为他教我如何使用上下文将值传递到animationDidStop。他还像您一样建议了基于块的动画。我将使用这些,我很高兴它的存在,尽管我需要更多地了解正确的代码才能让它工作 - 谢谢!
    • 一个注意事项要记住,我刚刚在网上找到 - 这仅在 iOS4 中可用,因此如果有人运行 3.2 的 iPad 或 iOS4 之前的 iPhone,这可能会使应用程序崩溃。真不幸。
    猜你喜欢
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多