2

播放多个 UIView 动画块时出现问题。

我所拥有的是一个有很多按钮的游戏,当你按下一个按钮时,它会产生一个 UILabel,它会在屏幕顶部动画到总点数,这是通过使用 3 个嵌套块来完成的。这很好用,让我尽可能多地生成带有动画的点标签。

    //setup the label
    UILabel *pointIndicator = [[UILabel alloc]initWithFrame:CGRectMake(button.button.frame.origin.x, button.button.frame.origin.y, 60, 30)];
    pointIndicator.backgroundColor = [UIColor clearColor];
    pointIndicator.font = [UIFont boldSystemFontOfSize:25];
    pointIndicator.alpha = 0;
    pointIndicator.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
    pointIndicator.layer.shadowOpacity = 0.6;
    pointIndicator.layer.shadowRadius = 1;

    [pointIndicators addObject:pointIndicator];
    [self.view addSubview:pointIndicator];


    CGPoint scoreLabelPosition = [self.view convertPoint:self.totalScoreLabel.frame.origin fromView:self.totalScoreLabel.superview];
    CGSize scoreLabelSize = [self.totalScoreLabel.text sizeWithFont:self.totalScoreLabel.font];

    [UILabel animateWithDuration:0.3 delay:0 options:UIViewAnimationCurveEaseIn animations:^{

        //Make label appear and move it above button
        CGRect frame = pointIndicator.frame;
        frame.origin.y -= 30;
        pointIndicator.frame = frame;
        pointIndicator.alpha = 1;

    }completion:^(BOOL finished){


        [UILabel animateWithDuration:0.4 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{

            //Move the label next to the score label
            NSInteger YPosition = 0;
            if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){

                YPosition = 15;

            }else{

                YPosition = 2;
            }
            CGRect frame = pointIndicator.frame;
            frame.origin.x = scoreLabelPosition.x + self.totalScoreLabel.frame.size.width/2 + scoreLabelSize.width/2 + 5;
            frame.origin.y = scoreLabelPosition.y - self.totalScoreLabel.frame.size.height/2 + YPosition;
            pointIndicator.frame = frame;

        }completion:^(BOOL finished){

            [UILabel animateWithDuration:0.5 animations:^{

                pointIndicator.alpha = 0;

            }completion:^(BOOL finished){

                //Animate point label to increase a bit in size
                CABasicAnimation *pointAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
                pointAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                pointAnim.duration = 0.1;
                pointAnim.repeatCount = 1;
                pointAnim.autoreverses = YES;
                pointAnim.removedOnCompletion = YES;
                pointAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)];
                [self.totalScoreLabel.layer addAnimation:pointAnim forKey:nil];

                [pointIndicator removeFromSuperview];
                [pointIndicators removeObject:pointIndicator];
            }];


        }];

    }];

然而,当游戏结束时,所有的按钮都在屏幕上显示动画,并且图像视图的大小从中间变大,每个按钮都使用 1 个动画块。

问题在于,如果 UILabel 在游戏结束的同时在屏幕上显示动画,它会取消按钮动画和不断增长的图像动画。如果没有生成 UILabel,则一切都按原样播放。在播放其他动画之前,我尝试取消并删除所有点标签,但没有运气。只有在之前的所有动画都提前正常完成的情况下,它似乎才起作用。这就是我删除它们的方式:

for(UIView *pointView in pointIndicators){

    [pointView.layer removeAllAnimations];
    [pointView removeFromSuperview];
}
[pointIndicators removeAllObjects];
[self.totalScoreLabel.layer removeAllAnimations];

所有动画视图都是同一个 UIView 的子视图。我注意到,如果我将按钮的点标签子视图设为所有动画都可以同时播放。

我似乎无法弄清楚这种行为,动画之间是否存在某种冲突?

4

0 回答 0