等待UIView动画循环完成的最佳方法是什么? [英] What is the best way to wait for a loop of UIView animations to finish?
问题描述
我试图遍历多个 UIViews
并在每个UIViews上执行动画,但是我想知道所有动画何时完成。动画循环结束后,调用函数的最佳方法是什么?或者,有没有办法等到全部完成?
I'm trying to loop over multiple UIViews
and perform animation on each, but I'd like to know when all of the animations have finished. What is the best way to call a function when the loop of animations has finished? Alternately, is there a way to wait until all have finished?
我尝试使用 setAnimationDidStopSelector
,但不会触发。在这种情况下,我要清除游戏板上的一些碎片,使它们淡出然后被去除(NumberedChipView是UIView的子类)。直到筹码脱离板面,比赛才能继续进行。
I tried using setAnimationDidStopSelector
, but it doesn't fire. In this case, I'm clearing some "chips" off a game board by having them fade out then get removed (NumberedChipView is a subclass of UIView). Play cannot continue until the chips are off the board.
[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
无济于事:
I also tried CATransaction
to no avail:
[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];
更新:
我现在可以触发选择器,但是它不等待动画循环结束。
I now can get the selector to fire, however it doesn't wait for the loop of animations to finish.
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
推荐答案
以下是对我有用的。我意识到UIView animateWithDuration方法没有与begin / commit部分相关联。相反,begin / commit部分标记了隐式动画操作的区域,因此我可以在动画中设置属性,然后将其隐式动画化。
Here's what ended up working for me. I realized that the UIView animateWithDuration methods weren't being associated to the begin/commit section. Rather, the begin/commit sections mark a region of "implicit" animation operations, so I could just set the properties within the animation and it would be implicitly animated.
例如,在下面的动画中,我只是将chipView的alpha属性设置为 0.0,并且chipView(UIView)被隐式设置为动画以消失。
For example, within the animation below, I simply set the alpha property of the chipView to "0.0" and the chipView (a UIView) is implicitly animated to disappear.
[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方法中,执行清理并从超级视图中删除筹码。仅当循环中的所有chipView动画完成时才触发此animationDidStop方法,这是我试图弄清楚的棘手事情。
Then, in my animationDidStop method, I do the "cleanup" and remove and chips from the superview. This animationDidStop method only gets fired when all chipView animations from the loop have finished, which is the tricky thing I was trying to figure out.
- (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];
}
}
}
这篇关于等待UIView动画循环完成的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!