按顺序播放多个动画

时间:2013-07-06 16:13:02

标签: ios uiview uiviewanimation

我正在尝试按顺序执行球爆炸效果。一个接一个。

我做了什么:

对于我曾经使用的球爆炸效果

UIButton *ballButton = (UIButton *)[cell viewWithTag:10];

ballButton.imageView.animationImages = [[NSArray alloc] initWithObjects:
                                        [UIImage imageNamed:@"1.png"],
                                        [UIImage imageNamed:@"2.png"],
                                        [UIImage imageNamed:@"3.png"],
                                        [UIImage imageNamed:@"4.png"],
                                        nil];
ballButton.imageView.animationDuration = 1;
ballButton.imageView.animationRepeatCount = 1;

并且代码上的这一行附加到集合视图的单元格中的多个按钮。 我称这些ballbutton.imageview像这样开始动画

[UIView animateWithDuration:1 delay:5 options:UIViewAnimationOptionCurveEaseOut animations:^{
        NSIndexPath *path2 = [NSIndexPath indexPathForRow:x inSection:0];
        UICollectionViewCell *cell = [ballContainer cellForItemAtIndexPath:path2];
        UIButton *ballObject = (UIButton *) [cell viewWithTag:10];
        [ballObject.imageView startAnimating];
    } completion:^(BOOL b){
          NSLog(@" here i call next animation of ball blast to execute ");
}];

我嵌套了3个动画按钮。

2 个答案:

答案 0 :(得分:0)

首先,为什么不为其他三个创建一个大动画? UIView animateWithDuration:的事情是它在你给它的时间段内执行动画块,即将帧从(200,200)设置为(0,0)将在一秒钟内按比例移动它,在你的情况下。但UIImageView关于动画的属性是以动画已经为你完成的方式制作的。

就个人而言,我建议使用计时器,如下所示:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:ballObject.imageView.animationDuration
                                                  target:self 
                                                selector:@selector(performNextAnimation:) 
                                                userInfo:nil repeats:NO];
[ballObject.imageView startAnimating];

performNextAnimation方法中:

- (void) performNextAnimation{
[timer invalidate]; // you have to access the timer you've scheduled with the animation
timer = nil;
/* code for starting the next animation */
}

答案 1 :(得分:0)

这样我解决了我的问题。 最初我通过调用

开始动画
        [self startAnim:index];

比这更实现了这个StartAnim并解决了问题。

-(void)StartAnim :(int)x{

    NSIndexPath *path2 = [NSIndexPath indexPathForRow:x inSection:0];
    UICollectionViewCell *cell = [ballContainer cellForItemAtIndexPath:path2];
    UIButton *ballObject = (UIButton *) [cell viewWithTag:10];
    [ballObject setBackgroundImage:nil forState:UIControlStateNormal];
    ballObject.imageView.image = nil;
    [ballObject.imageView startAnimating];

    double delayInSeconds = 0.15;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        if(x-6>=0){
            [self StartAnim :(x-6)];
        }
    });



}