什么是等待的UIView动画的循环来完成的最好方法是什么?(What is the best way

2019-09-17 06:54发布

我试图遍历多个UIViews和各执行动画,但我想知道,当所有的动画已经完成。 什么是调用函数时动画的循环已经结束的最好方法? 或者,有没有办法等到所有已经完成?

我试着用setAnimationDidStopSelector ,但它不火。 在这种情况下,我通过让他们淡出然后遭到移除清除一些“筹码”关游戏板(NumberedChipView是的UIView的子类)。 游戏无法继续,直到芯片了董事会。

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)]; 

// clear chips 
for (HexagonalTile *aTile in tilesToClear) {

    NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];

    [UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationCurveLinear animations:^{
        chipView.alpha = 0.0; 
    } 
        completion:^(BOOL finished){

         if (finished) {

             [chipView removeFromSuperview]; 
             [self.chipViews removeObject:chipView];

         }
     }]; 
}

[UIView commitAnimations]; 

我也试过CATransaction无济于事:

[CATransaction begin];  

[CATransaction setCompletionBlock:^{
    [self animationFinished]; 
}];  

// clear chips 
for (HexagonalTile *aTile in tilesToClear) {

    NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];

    [UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationCurveLinear animations:^{
        chipView.alpha = 0.0; 
    } 
        completion:^(BOOL finished){

         if (finished) {

             [chipView removeFromSuperview]; 
             [self.chipViews removeObject:chipView];

             //NSLog(@"clearing finished"); 
         }
     }]; 
}

[CATransaction commit]; 

更新:

我现在可以得到选择火,但它不等待动画完成的循环。

[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];

Answer 1:

您的循环发射了都具有相同的延迟和持续时间相同的动画,所以他们都将开始和结束在同一时间。

您现在的完成方法会为每个动画调用一次。

可以略微重写代码进入循环之前,计数器设置为零,并且每个步骤后加一。 当该指数小于阵列数,传递一个零完成块。 一旦指数==数组算,你在最后一个项目,因此通过与要调用一次最后的动画完成的代码完成块。

请注意,您可以使用此方法的索引,使每个动画开始在不同的时间,通过增加基于索引值的延迟量:

delay = .3 + (0.5 * index);


Answer 2:

以下是最终为我工作。 我意识到了UIView animateWithDuration方法并没有被关联到了开始/提交部分。 相反,开始/提交节纪念“隐含的”动画操作的区域,所以我可以设定动画中的属性,它会被隐式动画。

例如,下面的动画内,我简单设置chipView到“0.0”和chipView(一个UIView)被隐式动画消失的alpha属性。

[UIView beginAnimations:@"tilescleared" context:nil]; 
[UIView setAnimationDelegate: self];
[UIView setAnimationDelay:0.3]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

for (HexagonalTile *aTile in tilesToClear) {

    NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];

    chipView.alpha = 0.0; 

}

[UIView commitAnimations];

然后,在我的animationDidStop方法,我从上海华盈的“清理”,并删除和芯片。 这animationDidStop方法只能被炒鱿鱼时从循环中的所有chipView动画已经完成,这是我试图找出棘手的事情。

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

    if ([animationID isEqualToString:@"tilescleared"]
                  && [finished boolValue]) {

       for (HexagonalTile *aTile in self.animationInfo.tilesToClear) {

           NumberedChipView *chipView = [self chipViewForColumn:aTile.column andRow:aTile.row];

           [chipView removeFromSuperview]; 
           [self.chipViews removeObject:chipView];
       }

   }

}


文章来源: What is the best way to wait for a loop of UIView animations to finish?